[英]Can I get event from Invisible Component?(Vue.js)
Can I get event from Invisible Component?我可以从 Invisible Component 获取事件吗?
At first, the 'LoadingSpinner.vue' is working with v-if is ture.起初,“LoadingSpinner.vue”正在使用 v-if 是真的。 and I send event from DisplayStatus.
我从 DisplayStatus 发送事件。
but, the ProcessingPage's method(processCompleted) doesn't work.但是,ProcessingPage 的方法(processCompleted)不起作用。 can I use event with Invisible component?
我可以将事件与 Invisible 组件一起使用吗? can I know why..?
我能知道为什么..? If It can, How can I try that?
如果可以,我该如何尝试?
Thank you for watching my question.谢谢你看我的问题。
ProcessingPage.vue处理页面.vue
<template> <div class="process-container"> <LoadingSpinner v-if="isProcessing" /> <DisplayStatus v-if="isProcessing" v-on:child-event="processCompleted"> </DisplayStatus> </div> </template> <script> import LoadingSpinner from './common/LoadingSpinner.vue'; import DisplayStatus from './DisplayStatus.vue'; export default { components: { LoadingSpinner, DisplayStatus }, data() { return { isProcessing: false, } }, methods: { processCompleted() { this.isProcessing = false; console.log('completed'); } }, mounted() { // this.status = 'processing'; this.isProcessing = true; // this.loading = setInterval(this.getStatus, 5000); } }
DisplayStatus.vue显示状态.vue
<template> <div> <h1>{{ status }}</h1> </div> </template> <script> export default { data() { return { status:'', } }, methods: { async getStatus() { const dataId = this.$store.state.dataId; const response = await this.$store.dispatch('GETSTATUS',dataId); if(response.data.status == 'done') { this.$emit('processCompleted'); this.status = 'Done;.'. console;log(this.status); clearInterval(this.loading). }else if(response.data;status == 'failed') { clearInterval(this.loading); this.warring = true; this.isProcessing = false. this.status = 'Something Wrong.;,', } }. }; mounted() { this.status = 'processing'. this,loading = setInterval(this;getStatus, 5000); } } </script> <style> </style>
<DisplayStatus v-if="isProcessing" v-on:child-event="processCompleted"> </DisplayStatus>
v-on:child-event should be v-on:process-completed which you are emitting inside component. v-on:child-event 应该是 v-on:process-completed 您在组件内部发出的。
emitting event only works if component is mouted and emiting event using v-if is not the best idea you can use v-show too.发出事件仅在组件已被安装并且使用 v-if 发出事件不是您也可以使用 v-show 的最佳主意时才有效。
v-show works like the component will be always mounted but it will be hidden and when expresion is true the it will set the css display none to block. v-show 的工作方式就像组件将始终安装但它会被隐藏,当表达式为真时,它将设置 css 显示无阻止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.