admin管理员组文章数量:1332873
for a school project I have to build a vue js app.
I used emit and :v-on to exchange data and trigger methods from child ponent and it works great!, now I want to do the same thing from a child posant, that is a vue. That means it is a a ponent displayed by the router.
here is the child ponent:
methods: {
logout () {
console.log("logout child")
this.$emit('logoutUser', this.user)
}
},
and the app.vue:
<router-view v-if="this.user.isConnected && this.user.username != ''" :userProp="user" v-on:logout="logoutUser"></router-view>
[...]
logoutUser(user) {
console.log("logout App.vue", user)
}
I got the log from the child: "logout child", but I can't access the method logoutUser in App.vue... the "logout App.vue" log never show...
Do you know were I did a mistake? I'm pretty much a noob
Thanks!
for a school project I have to build a vue js app.
I used emit and :v-on to exchange data and trigger methods from child ponent and it works great!, now I want to do the same thing from a child posant, that is a vue. That means it is a a ponent displayed by the router.
here is the child ponent:
methods: {
logout () {
console.log("logout child")
this.$emit('logoutUser', this.user)
}
},
and the app.vue:
<router-view v-if="this.user.isConnected && this.user.username != ''" :userProp="user" v-on:logout="logoutUser"></router-view>
[...]
logoutUser(user) {
console.log("logout App.vue", user)
}
I got the log from the child: "logout child", but I can't access the method logoutUser in App.vue... the "logout App.vue" log never show...
Do you know were I did a mistake? I'm pretty much a noob
Thanks!
Share Improve this question asked Nov 3, 2021 at 14:26 AntoineAntoine 1171 silver badge12 bronze badges 1-
1
You are listening for
logout
event instead oflogoutUser
– Vasile Radeanu Commented Nov 3, 2021 at 14:37
1 Answer
Reset to default 6You have to change your v-on:logout
in the router-view element to v-on:logoutUser
, the v-on has to have the same value the $emit has. Also you could shorten your code a little with using @ instead of v-on: (so @logoutUser="logoutUser")
本文标签: javascriptemit data from a router view to the AppvueStack Overflow
版权声明:本文标题:javascript - emit data from a router view to the App.vue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742320337a2452657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论