【vue】報錯:mapState is not defined

場景:

vue-cli項目引入了vuex,在vue component模塊中調用以下代碼報錯。

export default {
	computed:{
		...mapState({
			isLogin: state => state.isLogin,
			userName:state => state.userName,
		}),
	}
}

錯誤:

App.vue?26cd:42 Uncaught ReferenceError: mapState is not defined
    at eval (App.vue?26cd:42)
    at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue (app.js:222)
    at __webpack_require__ (common.js.js:713)
    at fn (common.js.js:118)
    at eval (App.vue?9e04:1)
    at Object../src/App.vue (app.js:2463)
    at __webpack_require__ (common.js.js:713)
    at fn (common.js.js:118)
    at eval (main.js?1c90:1)
    at Object../src/main.js (app.js:2553)

 

解決辦法:

在vue component引入對應的方法,如以下的代碼

import {mapState,mapMutations,mapAction } from 'vuex'
export default {
	computed:{
		...mapState({
			isLogin: state => state.isLogin,
			userName:state => state.userName,
		}),
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章