实现原理:
存在问题:
解决以上问题:
// 给对象新增属性
this.$set(this.person,'sex','女')
// 删除对象的name属性
this.$delete(this.person,'name')
// 修改数组的第一个元素
this.$set(this.arr,0,'xxx')

在子组件中预留插槽,父组件可以把内容放到插入到插槽中
name 的 <slot> 出口会带有隐含的名字“default”v-slot 指令命名<template> 中的内容都会被视为默认插槽的内容
为了让 user 在父级的插槽内容中可用,我们可以将 user 作为 <slot> 元素的一个 attribute 绑定上去:
<span>
<slot v-bind:user="user"> {{ user.lastName }} </slot>
</span>
绑定在 <slot> 元素上的 attribute 被称为插槽 prop。现在在父级作用域中,我们可以使用带值的 v-slot 来定义我们提供的插槽 prop 的名字:
<current-user>
<template v-slot:default="slotProps"> {{ slotProps.user.firstName }} </template>
</current-user>
在这个例子中,我们选择将包含所有插槽 prop 的对象命名为 slotProps,但你也可以使用任意你喜欢的名字。
跟 v-on 和 v-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header:
<base-layout>
<template #header>
<h1>Here might be a page title</h1>
</template>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template #footer>
<p>Here's some contact info</p>
</template>
</base-layout>
然而,和其它指令一样,该缩写只在其有参数的时候才可用。这意味着以下语法是无效的:
<!-- 这样会触发一个警告 -->
<current-user #="{ user }"> {{ user.firstName }} </current-user>
<!-- 如果你希望使用缩写的话,你必须始终以明确插槽名取而代之: -->
<current-user #default="{ user }"> {{ user.firstName }} </current-user>
watch
computed
computed: {
aDouble: vm => vm.a * 2;
}
computed: {
// 计算属性的 getter
sum () {
// `this` 指向 vm 实例
return this.num1 + this.num2
}
}

watch:{
handler (newVal, oldVal) {
// 当 `a` 变化时调用
},
immediate: false // 立即执行一次
deep: true // 深度监听 如果要监听的数据的层级很多
}
data() {
return {
a: {
b: {
c: 1,
},
},
};
},
watch: {
// watch可以用 . 操作符监听对象里面的属性
// 如果只想监听对象里面 c 的值,不要用深度监听
"a.b.c": {
handler: function (val, oldVal) {
console.log(val, oldVal);
},
},
},

// 修改数据
vm.msg = "Hello";
// DOM 还没有更新
Vue.nextTick(function () {
// DOM 更新了
});
// 作为一个 Promise 使用
Vue.nextTick().then(function () {
// DOM 更新了
});