核心:
根据案例中所涉及的知识点,可以将其分为三个部分
1、内容绑定,事件绑定
2、显示切换,属性绑定
3、列表循环,表单元素绑定
设置标签的文本值(textContent)
语法:
<div id="app">
<h2 v-text="message">h2>
div>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!"
}
})
直接将message的值替换到h2的标签内部,但是这么做有一个缺点:message中有什么内容就会全部都被替换掉,如果我们只想要设置部分的内容,可以使用另外一种写法。
具体实例:
实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-test指令title>
head>
<div id="app">
<h2 v-text="message">h2>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!!!",
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-test指令title>
head>
<div id="app">
<h2 v-text="message">h2>
<h2 v-text="info">h2>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!!!",
info:"前端与移动教育部"
}
})
script>
body>
html>
使用两个大括号的方式{{ }},在标签的内部进行设置,这种写法也叫做cha值表达式。使用这种写法,只有大括号内部的值被替换,其他部分的内容都会原封不动的被保留。如果想涉及到一些逻辑代码。
语法:
<div id="app">
<h2 v-text="message">h2>
<h2 >深圳{{message}}h2>
div>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!"
}
})
具体实例:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-test指令title>
head>
<div id="app">
<h2 v-text="message">深圳h2>
<h2 v-text="info">深圳h2>
<h2>{{message}}深圳h2>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!!!",
info:"前端与移动教育部"
}
})
script>
body>
html>
如果想涉及到一些逻辑代码,还支持写表达式。
比如:字符串的拼接:拼接一个"!"
在插值表达式中,单引号和双引号都支持使用,如果两边用了双引号了,就要用单引号了。
语法:
<div id="app">
<h2 v-text="message+'!'">h2>
<h2 >深圳{{message+'!'}}h2>
div>
具体实例:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-test指令title>
head>
<div id="app">
<h2 v-text="message+'!'">深圳h2>
<h2 v-text="info+'!'">深圳h2>
<h2>{{message+'!'}}深圳h2>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!!!",
info:"前端与移动教育部"
}
})
script>
body>
html>
设置标签的innerHTML
语法:
<div id="app">
<p v-html="content">p>
div>
var app=new Vue({
el:"#app",
data:{
//content:"Hello Vue!" --和v-text效果没有区别
content:"<a href='#'>Hello Vue!a>"
}
})
具体实例:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-html指令title>
head>
<div id="app">
<p v-html="content">p>
<p v-text="content">p>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
content:"Hello Vue!"
}
})
script>
body>
html>
运行效果:
运行效果发现:这样写v-text和v-html显示没有区别
具体实例:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-html指令title>
head>
<div id="app">
<p v-html="content">p>
<p v-text="content">p>
div>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
//content:"Hello Vue!"
content:"Hello Vue!"
}
})
script>
body>
html>
运行效果:
点击Hello Vue!,可进行页面跳转
从上面这个实例就看出v-text和v-html的区别了。
总结
为元素绑定事件
语法:
<div id="app">
<input type="button" value="事件绑定" v-on:事件名="方法">
<input type="button" value="事件绑定" v-on:click="dolt">
<input type="button" value="事件绑定" v-on:mouseenter="dolt">
<input type="button" value="事件绑定" v-on:dblclick="dolt">
<input type="button" value="事件绑定" @dblclick="dolt">
div>
var app=new Vue({
el:"#app",
methods:{
doIt:funtion(){
//逻辑
}
},
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令title>
head>
<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
methods: {
doIt:function(){
alert("做It");
}
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令title>
head>
<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
<input type="button" value="v-on简写" @click="doIt">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
methods: {
doIt:function(){
alert("做It");
}
},
})
script>
body>
html>
运行结果:
两种方式都可以,简写方式代码量少一点,使用频率更高一点。
具体实例3:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令title>
head>
<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
<input type="button" value="v-on简写" @click="doIt">
<input type="button" value="双击事件" @dblclick="doIt">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
methods: {
doIt:function(){
alert("做It");
}
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令title>
head>
<body>
<div id="app">
<input type="button" value="v-on指令" v-on:click="doIt">
<input type="button" value="v-on简写" @click="doIt">
<input type="button" value="双击事件" @dblclick="doIt">
<h2 @click="changeFood">{{food}}h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
food:"西兰花炒蛋"
},
methods: {
doIt:function(){
alert("做It");
},
changeFood:function(){
//console.log(this.food);
this.food+="好好吃!"
}
},
})
script>
body>
html>
步骤分析:
1、data中定义数据:比如num
2、methods中添加两个方法:比如add(递增),sub(递减)
3、使用v-text将num设置给span标签
4、使用v-on将add,sub分别绑定给+,-按钮
5、累加的逻辑:小于10累加,否则提示
6、递减的逻辑:大于0递减,否则提示
var app=new Vue({
el:"#app",
data:{
num:1
},
methods: {
add:function(){},
sub:function(){}
},
})
<div id="app">
<button @click="sub">button>
<span>{{num}}span>
<button @click="add">button>
div>
具体代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算器title>
head>
<body>
<div id="app">
<button @click="sub">
-
button>
<span>{{num}}span>
<button type="+" @click="add">
+
button>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
num:1
},
methods:{
add:function(){
//console.log('add');
if(this.num<10)
{
this.num++;
}
else{
alert('别点啦,最大啦!')
}
},
sub:function(){
//console.log('sub');
if(this.num>0){
this.num--;
}
else{
alert('别点啦,最小啦!')
}
}
},
})
script>
body>
html>
根据表达值的真假,切换元素的显示和隐藏:
true:显示
false:隐藏
语法:
<div id="app">
<img src="地址" v-show="true">
<img src="地址" v-show="isShow">
<img src="地址" v-show="age>=18">
div>
var app=new Vue({
el:"#app",
data:{
isShow:false,
age:16
}
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-show指令title>
head>
<body>
<div id="app">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="true">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="isShow">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="age>=18">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false,
age:16
}
})
script>
body>
html>
运行结果:
本来显示3张照片,但是后两张为false,隐藏了。
具体实例2:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-show指令title>
head>
<body>
<div id="app">
<input type="button" value="切换显示状态" @click="changeIsShow">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="isShow">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false,
age:16
},
methods:{
changeIsShow:function(){
this.isShow=!this.isShow;
}
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-show指令title>
head>
<body>
<div id="app">
<input type="button" value="切换显示状态" @click="changeIsShow">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="isShow">
<img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg" v-show="age>=18">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false,
age:18
},
methods:{
changeIsShow:function(){
this.isShow=!this.isShow;
}
}
})
script>
body>
html>
运行结果:
点击按钮后:
总是显示的是逻辑是age>=18的那张图片
总结
根据表达式的真假,切换元素的显示和隐藏(操纵dom元素)
语法:
<div id="app">
<p v-if="true">我是一个p标签p>
<p v-if="isShow">我是一个p标签p>
<p v-if="表达式">我是一个p标签p>
div>
var app=new Vue({
el:"#app",
data:{
isShow:"false"
}
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-if指令title>
head>
<body>
<div id="app">
<input type="button" value="切换显示" @click="toggleIsShow">
<p v-if="isShow">我是一个p标签p>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false
},
methods:{
toggleIsShow:function(){
this.isShow=! this.isShow;
}
},
})
script>
body>
html>
运行结果:
点击按钮后:
具体实例2:v-if 和v-show的区别
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-if指令title>
head>
<body>
<div id="app">
<input type="button" value="切换显示" @click="toggleIsShow">
<p v-if="isShow">我是一个p标签p>
<p v-show="isShow">我是一个p标签-v-show修饰p>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false
},
methods:{
toggleIsShow:function(){
this.isShow=! this.isShow;
}
},
})
script>
body>
html>
运行结果:
点击按钮后:
区别:v-if是通过控制dom节点的存在与否来控制元素的显隐;v-show是通过设置dom元素的display样式,block为显示,none为隐藏;频繁切换的元素用v-show,反之用v-if。
具体实例3:表达式
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-if指令title>
head>
<body>
<div id="app">
<input type="button" value="切换显示" @click="toggleIsShow">
<p v-if="isShow">我是一个p标签p>
<p v-show="isShow">我是一个p标签-v-show修饰p>
<h2 v-if=" temperature>=35">热死啦h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
isShow:false,
temperature:40
},
methods:{
toggleIsShow:function(){
this.isShow=! this.isShow;
}
},
})
script>
body>
html>
设置元素的属性(比如:src,title,class)
v-bind:属性名=表达式
语法:
<div id="app">
<img v-bind:src="imgSrc">
<img :src="imgSrc">
<img v-bind:title=" imgTitle+'!!!">
<img v-bind:class="active?'active':''">
<img v-bind:class="{active:?isActive}">
div>
var app=new Vue({
el:"#app",
data:{
imgSrc:"图片地址",
imgTitle:"Hello Vue!",
isActive:false
}
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-bind指令title>
head>
<body>
<div id="app">
<img v-bind:src="imgSrc">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
imgSrc:"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg",
imgTitle:"Hello Vue!",
isActive:false
}
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-bind指令title>
head>
<body>
<div id="app">
<img v-bind:src="imgSrc">
<img :src="imgSrc">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
imgSrc:"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg",
imgTitle:"Hello Vue!",
isActive:false
}
})
script>
body>
html>
运行结果:
具体实例3:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-bind指令title>
head>
<body>
<div id="app">
<img v-bind:src="imgSrc" >
<img :src="imgSrc" :title="imgTitle+'!!!'">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
imgSrc:"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg",
imgTitle:"Hello Vue!",
isActive:false
}
})
script>
body>
html>
运行效果:
第一张图片没有加title,所以没有效果,第二张图片加了title,所以有效果,具体效果见第二张图片的中的白色框中的内容。
先暂时跳过
根据数据生成列表结构
语法
<div id="app">
<ul>
<li v-for="(item,index)in arr" :title="item">
{{index}}{{item}}
li>
<li v-for="(item,index)in objArr">
{{item.name}}
li>
ul>
div>
item是元素名称,可以任意起
var app=new Vue({
el:"#app",
data:{
arr:[1,2,3,4,5],
objArr:[
{name:"jack"},
{name:"rose"}
]
},
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for指令title>
head>
<body>
<div id="app">
<ul>
<li v-for="item in arr" >
大城市:{{item}}
li>
ul>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
},
})
script>
body>
html>
运行结果:
具体实例2:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for指令title>
head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in arr">
{{index}}大城市:{{item}}
li>
ul>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"]
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for指令title>
head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in arr">
{{index+1}}大城市:{{item}}
li>
ul>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"]
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for指令title>
head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in arr">
{{index+1}}大城市:{{item}}
li>
ul>
<h2 v-for="item in vegetables" v-bind:title="item.name">
{{item.name}}
h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
vegetables:[
{name:"西兰花炒蛋"},
{name:"蛋炒西兰花"}
]
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for指令title>
head>
<body>
<div id="app">
<input type="button" value="添加数据" @click="add">
<input type="button" value="移除数据" @click="remove">
<ul>
<li v-for="(item,index) in arr">
{{index+1}}大城市:{{item}}
li>
ul>
<h2 v-for="item in vegetables" v-bind:title="item.name">
{{item.name}}
h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
vegetables:[
{name:"西兰花炒蛋"},
{name:"蛋炒西兰花"}
]
},
methods:{
add:function(){
this.vegetables.push({name:"花菜炒蛋"});
},
remove:function(){
this.vegetables.shift();
}
}
})
script>
body>
html>
传递自定义参数,事件修饰符
语法:
<div id="app">
<input type="button" @click="doIt(p1,p2)"/>
<input type="text" @keyup.enter="sayHi">
div>
var app=new Vue({
el:"#app",
methods: {
doIt:function(p1,p2){},
sayHi:function(){}
},
})
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令补充title>
head>
<body>
<div id="app">
<input type="button" value="点击" @click="doIt(666,'老铁')">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
methods: {
doIt:function(p1,p2){
console.log(p1);
console.log(p2);
},
})
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on指令补充title>
head>
<body>
<div id="app">
<input type="button" value="点击" @click="doIt(666,'老铁')">
<input type="text" @keyup.enter="sayHi">
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
methods: {
doIt:function(p1,p2){
console.log(p1);
console.log(p2);
},
sayHi:function(){
alert("吃了没");
}
},
})
script>
body>
html>
获取和设置表单元素的值(双向数据绑定)
具体实例1:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-modle指令title>
head>
<body>
<div id="app">
<input type="text" v-model="message"/>
<h2>{{message}}h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!"
},
})
script>
body>
html>
运行结果:
文本框中输入什么,就显示什么,实现了双向绑定。
具体实例2:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-modle指令title>
head>
<body>
<div id="app">
<input type="text" v-model="message" @keyup.enter="getM">
<h2>{{message}}h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!"
},
methods:{
getM:function(){
alert(this.message);
}
}
})
script>
body>
html>
运行结果:
文本框输入后,点击回车
当你改变了表单元素的值,message值也同步改变。
具体实例3:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-modle指令title>
head>
<body>
<div id="app">
<input type="button" value="修改message" @click="setM">
<input type="text" v-model="message" @keyup.enter="getM">
<h2>{{message}}h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app=new Vue({
el:"#app",
data:{
message:"Hello Vue!"
},
methods:{
getM:function(){
alert(this.message);
},
setM:function(){
this.message="继续努力"
}
}
})
script>
body>
html>