简体   繁体   English

我可以从不可见的组件中获取事件吗?(Vue.js)

[英]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.

相关问题 如何在 Vue.js 中获取从子组件到父组件的 json 收集的数据? - How can I get json collected data from child to parent component in Vue.js? 将事件从子组件引发到 Vue.js 中的容器组件? - raising an event from a child component to a container component in Vue.js? 我正在使用 Vue.js 2.0 并且我试图从“子组件”发出一个事件 - I am using Vue.js 2.0 and I am trying to emit an event from `child component` 如何在使用Vue.js触发任何事件时创建一个新组件? - How can I create a new component on firing any event using Vue.js? 父级的Vue.js $ emit事件,并在组件(子级)上接收 - Vue.js $emit event from parent and receive it on a component (child) 如何从 Vue.js 功能组件发出事件? - How to emit an event from Vue.js Functional component? Vue.js 从未捕获的子组件发出事件 - Vue.js emit event from child component not catched 如何在不使用 vuex 的情况下将对象从 A 组件传递到 Vue.js 中的 B 组件? - How can I pass an Object from A component to B component in Vue.js without using vuex? 如何在 Vue.js 中将数据从父组件传递到子组件? - How can i pass data from parent component to a child component in Vue.js? 我想从 javascript create element with DOM on vue.js(nuxt.js) 获取事件@click - I want to get event @click from javascript create element with DOM on vue.js(nuxt.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM