支持内部写表达式
<div id="app">
<h2 v-text="message"></h2>
<h2 v-text="info"></h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
message: '11111',
info:'lalala'
}
})
</script>
v-text指令无论内容是什么,只会解析为文本
<div id="app">
<h2 v-html="content"></h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
// content:'programmer'
content:'<a href="http://www.bilibili.com">programmer</a>'
}
})
</script>
方法内部通过this关键字可以访问定义在data中的数据
<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="double click" @dblclick="doIt">
<h2 @click="changeFood">{{ food }}</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data: {
food: 'hot pot'
},
methods:{
doIt:function () {
alert(' do it ');
},
changeFood:function () {
this.food += ' delicious'
console.log(this.food)
}
}
})
</script>
</body>
.enter 可以限制触发的按键为回车
<body>
<div id="app">
<input type="button" value="click" @click="doIt(666,'lao tie')">
<input type="text" @keyup.enter="sayHi">
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
arr:['beijing','shanghai','guangzhou','shenzhen'],
food:[
{name: 'hot pot'},
{name: 'hot dry noodles'}
]
},
methods: {
doIt:function (p1,p2) {
console.log('do it')
console.log(p1)
console.log(p2)
},
sayHi:function () {
alert('岂料毛')
}
}
})
</script>
</body>
实例 - 计数器
<body>
<div id="app">
<div class="input-num">
<button @click="sub">
-
</button>
<span>{{ num }}</span>
<button @click="add">
+
</button>
</div>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data: {
num: 1
},
methods:{
add:function () {
if(++(this.num) > 10){
this.num = 10
alert('超过最大值')
}
},
sub:function () {
if(--(this.num) < 0){
this.num = 0
alert('超过最小值')
}
}
}
})
</script>
</body>
true -- 显示 , false -- 隐藏
<div id="app">
<input type="button" value="display" @click="changeIsShow">
<img v-show="isShow" src="img/read.png" alt="">
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
isShow:false,
age:18
},
methods:{
changeIsShow:function(){
this.isShow = ! this.isShow
}
}
})
</script>
<body>
<div id="app">
<input type="button" value="toggle" @click="toggleIsShow">
<p v-if="isShow">programmer</p>
<p v-show="isShow">programmer *</p>
<h2 v-if="temperature>=35">热死了</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
isShow:false,
temperature:40
},
methods:{
toggleIsShow:function(){
this.isShow = ! this.isShow
}
}
})
</script>
</body>
可简写成 :属性名
<body>
<div id="app">
<img :src="imgSrc" alt="" v-bind:title="imgTitle">
<img :src="imgSrc" alt="" v-bind:title="imgTitle" :class="isActive?'active':''" @click="toggleActive">
<img :src="imgSrc" alt="" v-bind:title="imgTitle" :class="{active:isActive}" @click="toggleActive">
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
imgSrc:'img/read.png',
imgTitle:'read',
isActive:false
},
methods:{
toggleActive:function(){
this.isActive = ! this.isActive
}
}
})
</script>
</body>
实例 - 图片切换
<body>
<div id="app">
<img :src="imgArr[index]" alt="">
<a href="#" @click="prevImg" v-show="index != 0" class="left">prev</a>
<a href="#" @click="nextImg" v-show="index != imgArr.length - 1" class="right">next</a>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
imgArr:[
"img/0.png",
"img/1.png",
"img/2.png",
"img/3.png"
],
index:0
},
methods: {
nextImg: function () {
++this.index
},
prevImg: function () {
--this.index
}
}
})
</script>
</body>
数组长度的更新会同步到页面上,是响应式的
<body>
<div id="app">
<input type="button" value="add" @click="add">
<input type="button" value="remove" @click="remove">
<ul>
<li v-for="(item,index) in arr">{{ index + 1 }} city: {{ item }}</li>
</ul>
<h2 v-for="item in food" :title="item.name">{{ item.name }}</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
arr:['beijing','shanghai','guangzhou','shenzhen'],
food:[
{name: 'hot pot'},
{name: 'hot dry noodles'}
]
},
methods: {
add:function () {
this.food.push({ name:'chao fan'})
},
remove:function () {
this.food.shift();
}
}
})
</script>
</body>
绑定的数据<-->表单元素的值
<body>
<div id="app">
<input type="text" v-model="message" @keyup.enter="getM">
<input type="button" value="edit message" @click="setM">
<h2>{{ message }}</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
var app = new Vue({
el:"#app",
data:{
message:'programmer'
},
methods: {
getM:function () {
alert(this.message)
},
setM:function () {
this.message = 'dkw'
}
}
})
</script>
</body>