[英]NativeScript Vue - VueX not working: cannot read property getters of undefined
I have a VueX module named auth
.我有一个名为auth
的 VueX 模块。
Here is the module:这是模块:
export const auth = {
namespaced: true,
state: {
user: null,
status: {
loggedIn: true
}
},
actions: {
},
mutations: {
},
getters: {
currentUser(state) {
return state.user;
},
loggedIn(state) {
return state.status.loggedIn;
}
}
}
I've left out the actions and mutations for the sake of brevity.为了简洁起见,我省略了动作和突变。 As you can see I have getters clearly defined.如您所见,我有明确定义的吸气剂。 I register the module in my store.ts file like so:我在我的 store.ts 文件中注册模块,如下所示:
import Vue from 'vue';
import Vuex from 'vuex';
import { auth } from './store/auth.module.js';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
auth
}
});
Now I wish to access the getters in a component so I tried mapGetters(['auth/loggedIn'])
& mapGetters('auth', ['loggedIn'])
in computed however I get errors for both.现在我希望访问组件中的 getter,所以我在计算中尝试mapGetters(['auth/loggedIn'])
和mapGetters('auth', ['loggedIn'])
,但是我都得到了错误。 Here's what it looks like:这是它的样子:
computed: {
...mapGetters(['auth/loggedIn'])
}
I even tried accessing the getter directly via this.$store.getters['auth/loggedIn']
however this did not work.我什至尝试通过this.$store.getters['auth/loggedIn']
直接访问 getter,但这不起作用。
Any idea about what could be the issue?关于可能是什么问题的任何想法?
EDIT: Here's the error after i run the tns preview
command:编辑:这是我运行tns preview
命令后的错误:
Webpack compilation complete. Watching for file changes.
Webpack build done!
Project successfully prepared (android)
Start sending initial files for device Nexus 6P (2e6517cd-cc2c-45a8-8915-525d6e437822).
Successfully sent initial files for device Nexus 6P (2e6517cd-cc2c-45a8-8915-525d6e437822).
LOG from device Nexus 6P: [Vue warn]: Error in created hook: "TypeError: Cannot read property 'getters' of undefined"
LOG from device Nexus 6P: '{NSVue (Vue: 2.6.10 | NSVue: 2.5.0)} -> CreateElement(NativeFrame)'
LOG from device Nexus 6P:
LOG from device Nexus 6P: An uncaught Exception occurred on "main" thread.
Unable to start activity ComponentInfo{org.nativescript.preview/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined
StackTrace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.preview/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined
at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
at com.tns.Runtime.callJSMethod(Runtime.java:1160)
at com.tns.Runtime.callJSMethod(Runtime.java:1138)
at com.tns.Runtime.callJSMethod(Runtime.java:1134)
at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
... 9 more
The issue was the import statement import Vue from "vue"
in file store.ts
问题是文件store.ts
中的 import 语句import Vue from "vue"
I changed it to import Vue from "nativescript-vue"
and now things seem to work.我将其更改为import Vue from "nativescript-vue"
,现在似乎一切正常。
I've created an issue here https://github.com/NativeScript/NativeScript/issues/8570 as the wrong import is added by following suggested getting started steps in the official website here: https://nativescript-vue.org/en/docs/getting-started/quick-start/我在这里创建了一个问题https://github.com/NativeScript/NativeScript/issues/8570因为错误的导入是通过以下官方网站中建议的入门步骤添加的: https://nativescript-vue.org/ zh/docs/入门/快速入门/
You have to define your state correctly to make getters
work.您必须正确定义 state 才能使getters
工作。
so you must have state like this:所以你必须有这样的 state :
state:{
user: null,
status: {
loggedIn: true
}
},
and then in your component:然后在您的组件中:
computed: {
...mapGetters({loggedIn: "auth/loggedIn"})
},
and now you have access to your state with this.loggedIn
现在您可以使用 this.loggedIn 访问您的this.loggedIn
UPDATE更新
You must add store to your vue instance too:您还必须将 store 添加到您的 vue 实例中:
const app = new Vue({
el:'#app',
store, // here
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.