I want to set data from a component to another component with vuejs. Here's my code:
http://quangtri.herokuapp.com/s/ByNN4FE-b
Where am I wrong? Please help me! Thank you very much!
相关问题
- Axios OPTIONS instead of POST Request. Express Res
- Vue.js - set slot content programmatically
- Simple vue app doesn't show anything
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
相关文章
- Best way to dynamically change theme of my Vue.js
- vue-router how to persist navbar?
- How to Properly Use Vue Router beforeRouteEnter or
- Difference between nameFunction() {} and nameFunct
- Vue - best way to reuse methods
- setTimeout() not working called from vueJS method
- How to unbind an array copy in Vue.js
- Vue Cli 3 Local fonts not loading
You are using a
function
in yourbus.$on
, sothis
is not what you think it is. Use an arrow function instead and it works.That's because in that piece of code,
this
is window, not the component.Also you are setting data using
this.$data.name
instead of simplythis.name
, I'm not sure about it, but this could result in reactive data issues.Solution:
If you are using ES6, you can do this:
This is called arrow function, and when using an arrow function instead of a normal function, the value of
this
inside the function will be the same as in the parent scope (so, the component instance who containsdata
).If you are using vanilla javascript, use
.bind(this)
at the end: