Recently working with VueJS - Vuex and I found the this keyword is confusing. Here the code in one of my components:
props: {
name: { type: String, default: '' }
},
computed: {
...mapState('storeJobNew', {
factors: (state) => state.job['majors']
})
},
It's work fine, until I want to make it dynamic, so the computed.factors is now base on the props.name value.
props: {
name: { type: String, default: '' }
},
computed: {
...mapState('storeJobNew', {
factors: (state) => state.job[this.name]
})
},
Now computed.factors is undefined. After a few hours of Googling, I found out that I shouldn't use arrow function here, and my code look like this:
props: {
name: { type: String, default: '' }
},
computed: {
...mapState('storeJobNew', {
factors: function (state) { return state.job[this.name] }
})
},
So here the question:
Why this keyword in arrow function not working here? (I thought that the this keyword here should be the Vue instance that I was created)
Why I need to write this.name instead of this.props.name to access the value in props?