Vue.js 如何在nativescript vue中管理用户的登录状态?
我想知道如何处理用户的登录/注销,所以我做了以下操作:Vue.js 如何在nativescript vue中管理用户的登录状态?,vue.js,nativescript,nativescript-vue,Vue.js,Nativescript,Nativescript Vue,我想知道如何处理用户的登录/注销,所以我做了以下操作: store.commit('load_state'); store.subscribe((mutations, state) => { ApplicationSettings.setString('store', JSON.stringify(state)); }); new Vue({ store, render: h => h('frame', [h(store.state.is_logged_in ? App
store.commit('load_state');
store.subscribe((mutations, state) => {
ApplicationSettings.setString('store', JSON.stringify(state));
});
new Vue({
store,
render: h => h('frame', [h(store.state.is_logged_in ? App : Login)]),
created() {
this.$store.commit('setNav', this.$navigateTo);
if (this.$store.state.is_logged_in) {
this.$store.dispatch('init');
}
},
}).$start();
请注意,loadstate最初从应用程序设置加载状态。
但此解决方案的问题是,Login.vue的子组件中没有此.$store
正确的方法是什么
请注意,我没有在这里使用vue路由器。我为这个问题苦苦挣扎了两天,直到最终找到了解决方案 我的问题是因为我在使用
$navigateTo
时没有指定框架,而是在导航整个组件。我发现store只绑定到main.js中传递给render函数的第一个组件
下面是我的main.js的样子:
new Vue({
store,
render: h => h('frame', [h(store.state.is_logged_in ? Main : Login)]),
created() {
this.$store.commit('setNav', this.$navigateTo);
if (this.$store.state.is_logged_in) {
this.$store.dispatch('init');
}
},
}).$start();
我发现如果is_logged_是真的this.$store
,或mapActions
等只在Main
及其子组件中工作,否则它只在Login
及其子组件中工作。然后我正在阅读,在示例的存储中看到了以下代码:
import Vue from 'nativescript-vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({...});
Vue.prototype.$store = store;
module.exports = store;
所以我添加了行Vue.prototype.$store=store代码>到我自己的商店定义,最后我的问题解决了。这真的让我很难过。希望我能救人