场景:

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,
		}),
	}
}

 

Logo

前往低代码交流专区

更多推荐