gpt4 book ai didi

Vue.js:有没有办法知道组件实例何时挂载?

转载 作者:行者123 更新时间:2023-12-03 06:39:35 25 4
gpt4 key购买 nike

是否有我们可以在每个组件实例中访问的 bool 值来了解码件何时安装?

就像是:

<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>

最佳答案

2020 年更新
Vue.js 有一个未记录的功能,您可以在其中知道组件的生命周期 Hook 何时执行:Source .
语法如下:

<ChildComponent @hook:lifecycleHookName="callAMethod" />
示例:
如何知道何时挂载子组件:
<ChildComponent @hook:mounted="componentMountedDoSomething" />
如何知道何时创建子组件:
<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>
和脚本:
export default {
data: () => ({
created: false,
mounted: false
}),
created () {
this.created = true
},
mounted() {
this.mounted = true
}
}

关于Vue.js:有没有办法知道组件实例何时挂载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55079866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com