this.$set() 方法 给对象/数组添加值或者替换值
语法:this.$set(目标对象/数组,键名/索引,新值)
使用1:可以给目标对象增加属性
使用2:可以给目标对象替换属性
使用3:可以给目标数组增加元素
使用4:可以给目标数组替换元素
let obj = {
name: '张三丰',
age: 12,
school: '中学'
}
this.$set(obj, 'age', 23)
console.log(obj)
this.$set(obj, 'address', '中国')
console.log(obj)
let arr = [1, 2, 3, 455, 32, 6, 7]
this.$set(arr, 3, 65)
console.log(arr)
this.$set(arr, arr.length, 23)
console.log(arr)
this.$delete() 删除对象中的属性,并保证改变视图中的变化
<!-- -->
<template>
<div>
<div v-for="(v,k,i) in obj" :key="v">{{v}}-{{k}}-{{i}}</div>
<button @click="delObj()">点击删除age</button>
</div>
</template>
<script>
export default {
name: 'VueOne',
data () {
return {
obj: {
name: '张天易',
age: 12,
sex: '男'
}
}
},
methods: {
delObj () {
this.$delete(this.obj, 'age')
}
},
created () {
},
mounted () {
}
}
</script>
<style scoped>
</style>
Vue.use() 实例化Vue.js插件
当我们需要使用需要 npm install 安装的vue插件的时候,就需要使用Vue.use()
比如 Vue.use()