vuex組件里打印this.$store.state為undefinedvuex組件里打印this.$store存在切state為虛的問題:我在組件怎么獲取state?謝謝
2 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
首先肯定得在main.js中引入store
main.js:
store/index.js
組件中就可以直接使用this.$store.state獲取state了。
一般都是在store通過getter返回store,組件中的計算屬性中獲取store的getters
從計算屬性里面拿到vuex里面的state數據就可以在組件里面直接用了

鳳凰求蠱
TA貢獻1825條經驗 獲得超4個贊
import { mapState } from 'vuex';
export default {
computed: {
...mapState({
direction: state => state.direction,//綁定store.state.direction到組件,之后可用this.direction獲取
}),
},
}
添加回答
舉報
0/150
提交
取消