v-bind(缩写是:)
1.用于绑定属性
<input type="button" v-bind:value="msg3">
var vm=new Vue({
el:'#app',
data:{
msg:'123',
msg2:"<h2>我是一个好学生</h2>",
msg3:"我是一个按钮"
}
})
6月23号更
另外v-bind还有一个重要的作用就是动态的切换class类型
具体见(https://cn.vuejs.org/v2/guide/class-and-style.html)绝对详细
我们可以传给 v-bind:class
一个对象,以动态地切换 class:
<div v-bind:class="{ active: isActive }"></div>
上面的语法表示 active
这个 class 存在与否将取决于数据属性 isActive
的 truthiness。
你可以在对象中传入更多属性来动态切换多个 class。此外,v-bind:class
指令也可以与普通的 class 属性共存。当有如下模板:
<div
class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }"
></div>
和如下 data:
data: {
isActive: true,
hasError: false
}
结果渲染为:
<div class="static active"></div>
当 isActive
或者 hasError
变化时,class 列表将相应地更新。例如,如果 hasError
的值为 true
,class 列表将变为 "static active text-danger"
。
绑定的数据对象不必内联定义在模板里:
<div v-bind:class="classObject"></div>
data: {
classObject: {
active: true,
'text-danger': false
}
}
渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:
<div v-bind:class="classObject"></div>
data: {
isActive: true,
error: null
},
computed: {
classObject: function () {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal'
}
}
}
v-on(缩写是@)
用于绑定事件
<div id="app">
<input type="button" v-on:click="show" value="弹出">
</div>
<script src="vue-2.5.17.js"></script>
<script>
var vm=new Vue({
el:'#app',
data:{
},
methods:{
show:function(){
alert("我是一个好学生");
}
}
})
</script>
method里面添加方法,这样点击就会弹出提示框
上面的input标签等同于:
<input type="button" @click="show" value="弹出">
跑马灯小案例:
<div id="app">
<input type="button" :value="info1" @click="move">
<input type="button" :value="info2" @click="stop">
<h3>{{msg}}</h3>
</div>
<script>
var flag = new Vue({
el: "#app",
data: {
info1: "动起来",
info2: "停下来",
msg: "我是一个好学生",
intval1:null,
start:"",
end:"",
intalId:null
},
methods: {
move() {
console.log(this.intalId);
if(this.intalId!=null) return;
intval1=setInterval(()=>{
start = this.msg.substring(0, 1)
end = this.msg.substring(1)
this.msg = end + start
this.intalId="1"
}, 200)
},
stop(){
clearInterval(intval1)
this.intalId=null
}
}
})
</script>
注意点:
clearInterval()里面不能写this.intval1