我们是否可以在每个组件实例中访问布尔值,以了解何时安装组件?
比如:
<template>
<div>
<span v-if="$mounted">I am mounted</span>
<span v-if="$created">I am created</span>
<span>Called before created and mounted</span>
</div>
</template>
我们是否可以在每个组件实例中访问布尔值,以了解何时安装组件?
比如:
<template>
<div>
<span v-if="$mounted">I am mounted</span>
<span v-if="$created">I am created</span>
<span>Called before created and mounted</span>
</div>
</template>
UPDATE 2020
Vue有一个未记录的功能.js,您可以知道何时执行组件的生命周期挂钩:Source.
The syntax is as follows:
<ChildComponent @hook:lifecycleHookName="callAMethod" />
Examples:
How to know when a child component is mounted:
<ChildComponent @hook:mounted="componentMountedDoSomething" />
How to know when a child component is created:
<ChildComponent @hook:created="componentCreatedDoSomething" />
<template>
<div>
<span v-if="mounted">I am mounted</span>
<span v-if="created">I am created</span>
<span>Called before created and mounted</span>
</div>
</template>
还有 playbook :
export default {
data: () => ({
created: false,
mounted: false
}),
created () {
this.created = true
},
mounted() {
this.mounted = true
}
}