关于vue.js:工作学习总结2

3次阅读

共计 665 个字符,预计需要花费 2 分钟才能阅读完成。

一、Vue.set 和 this.$set 的区别

受 ES5 的限度,Vue.js 不能检测到对象属性的增加或删除。因为 Vue.js 在初始化实例时将属性转为 getter/setter,所以属性必须在 data 对象上能力让 Vue.js 转换它,能力让它是响应的。
正确写法:this.$set(this.data,”key”,value’)

var vm=new Vue({
    el:'#test',
    data:{
        //data 中曾经存在 info 根属性
        info:{name:'小明';}
    }
});
// 给 info 增加一个性别属性
Vue.set(vm.info,'sex','男');

Vue.set 是将 set 绑定在 vue 的构造函数上,而 this.$set 是间接挂载在 vue 的原型上。

二、$on 和 $emit 事件

$emit 是触发事件 $on 是监听事件

// 监听 slotValueChange 事件
created() {this.$on('slotValueChange', this.slotValueChange);
},
slotValueChange() {this.$emit('change', this.values, this);
},

三、$(‘body’).off().on()

// 点击事件委托,向下冒泡,匹配到 button 的类则触发
$('body').on('click', '.button', (e) => {})

// 防止多个事件触发,能够在点击事件之前先移除以后 button 的所有事件,on 和 off 的参数须要保持一致
$('body').off('click', '.button')
正文完
 0