简体   繁体   English

MapState 在 MVC 项目中找不到 Vuex4 和 Vue3

[英]MapState not found Vuex4 with Vue3 in MVC project

In my MVC project i am using Vue3 with Vuex4 and they are working but i have got one issue with mapState.在我的 MVC 项目中,我将 Vue3 与 Vuex4 一起使用,它们正在工作,但我遇到了 mapState 的一个问题。

if I use these import statements i am getting this error:如果我使用这些导入语句,我会收到此错误:

import Vue from 'vue';
import Vuex from 'vuex';

1 Uncaught SyntaxError: Cannot use import statement outside a module 1 未捕获的语法错误:不能在模块外使用导入语句

So i add these files in my _Layout page to fix it所以我在我的 _Layout 页面中添加这些文件来修复它

<script src="~/lib/vue/vue.global.js"></script>
<script src="~/lib/vuex/vuex.global.js"></script>

So far it is working.到目前为止它正在工作。 But when i want to use mapState, i am stack!但是当我想使用 mapState 时,我是堆栈! not sure how to fix it,不知道如何解决,

import { mapState } from 'vuex';

I searched for mapStaate.js file but couldn't find or the files i find has just few lines of code and not working...我搜索了 mapStaate.js 文件但找不到,或者我找到的文件只有几行代码并且无法正常工作...

const store = new Vuex.Store({
state: {
    count: 0
   }
})

const homeIndex = {
data() {
    return {
    }
},
computed: mapState({
    count: state => state.count,
  })
}

Vue.createApp(homeIndex).mount("#app");

The import statement is for modules. import语句用于模块。 You don't need the module syntax since you are loading the scripts directly.您不需要模块语法,因为您是直接加载脚本。 Remove the import statement and use Vuex.mapState like you used Vuex.Store :删除import语句并像使用Vuex.mapState一样使用Vuex.Store

computed: Vuex.mapState({
   count: state => state.count,
})

If you prefer, you can use object destructuring to give a similar appearance to imports.如果您愿意,可以使用对象解构为导入提供类似的外观。 Then you could use mapState as you did in the question:然后你可以像你在问题中所做的那样使用mapState

const { mapState, mapGetters, mapActions } = Vuex;

computed: mapState({
   count: state => state.count,
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM