设置如下:
export default Vue.extend({
name: 'Home',
computed: {
...mapState('user', ['card']),
},
created() {
this.fetchData();
},
mounted() {
this.$once('dataLoaded', () => {
if (!this.card) {
this.showWarning();
}
});
},
watch: {
'$route': 'fetchData'
},
methods: {
async fetchData() {
await Promise.all([...]);
this.$emit('dataLoaded');
},
showWarning() {
// Vue global Plugin for creating a banner
Notify.create();
}
}
});
我在
mounted
生命周期中附加了侦听器,但是我想知道是否应该在created()
中进行监听。在这两种情况下似乎都可以正常工作,因此我想知道是否有一些最佳实践,或者我错过了重要的事情。谢谢!
最佳答案
您使事情变得过于复杂。
不需要emit
,因为您只向我们展示了一个组件
像这样做:
methods: {
async fetchData() {
await Promise.all([...]);
this.myMethod()
},
myMethod() {
if (!this.card) {
this.showWarning();
}
}