【问题标题】:how to acces getter from another js file如何从另一个js文件访问getter
【发布时间】:2020-08-25 13:20:42
【问题描述】:

考虑一个 laravel + vuejs 项目: 我想通过使用 store.getters.currentUser 在我的 Acces.js 文件中获取我的 currentUser 角色,但它没有显示给我

渲染错误:“TypeError:无法读取属性‘getters’ 未定义

我尝试了很多方法,但没有成功!有什么帮助吗??

这是我的 Acces.js:

import {store} from './store' 
export default class  Acces {


 Admin(){
     return store.getters.currentUser.role ==="admin";
 }
 NotUser(){
     return store.getters.currentUser.role === "admin" ||  store.getters.currentUser.role === "chef de projet" ||store.getters.currentUser.role === "client" ;
 }
 Chef(){
     return store.getters.currentUser.role === "chef de projet";
 }
 client(){
    return store.getters.currentUser.role === "client";
}
adminchef(){
    return store.getters.currentUser.role === "admin" || store.getters.currentUser.role === "chef de projet";
}
chefUser(){
    return store.getters.currentUser.role !== "admin" || store.getters.currentUser.role === "chef de projet" ||user.role !== "client"
}
};

还有这个 store.js:

import { getLocalUser } from "./helpers/auth";

const user = getLocalUser();

export default {
state: {
    currentUser: user,
    isLoggedIn: !!user,
    loading: false,
    auth_error: null,
    customers: []
},
getters: {
    isLoading(state) {
        return state.loading;
    },
    isLoggedIn(state) {
        return state.isLoggedIn;
    },
    currentUser(state) {
        return state.currentUser;
    },
    authError(state) {
        return state.auth_error;
    },
    customers(state) {
        return state.customers;
    }
},
mutations: {
    login(state) {
        state.loading = true;
        state.auth_error = null;
    },
    loginSuccess(state, payload) {
        state.auth_error = null;
        state.isLoggedIn = true;
        state.loading = false;
        state.currentUser = Object.assign({}, payload.user, {token: payload.access_token});

        localStorage.setItem("user", JSON.stringify(state.currentUser));
    },
    loginFailed(state, payload) {
        state.loading = false;
        state.auth_error = payload.error;
    },
    logout(state) {
        localStorage.removeItem("user");
        state.isLoggedIn = false;
        state.currentUser = null;
    },
    updateCustomers(state, payload) {
        state.customers = payload;
    }
},
actions: {
    login(context) {
        context.commit("login");
    },
    getCustomers(context) {
        axios.get('/api/customers')
        .then((response) => {
            context.commit('updateCustomers', response.data.customers);
        })
    }
}
};

【问题讨论】:

    标签: javascript vue.js ecmascript-6 vuex


    【解决方案1】:

    改变

    import {store} from './store' 
    

    import store from './store' 
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2022-01-22
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 2020-02-09
      • 2019-11-10
      • 2015-05-17
      • 2013-06-09
      相关资源
      最近更新 更多