【问题标题】:[Vue warn]: Error in render: "TypeError: Cannot read property 'getters' of undefined"[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘getters’”
【发布时间】:2021-08-18 11:23:43
【问题描述】:

这是我的 app.js 我是否正确定义了项目? 我认为我的工作做得对!按照vuex文档

import store from "./store";
require('./bootstrap');
window.Vue = require('vue').default;
import vuetify from "./vuetify";
import router from "./router";
import AppComponent from "./components/AppComponent";
import Store from  "./store"
const app = new Vue({
  el: '#app',
  vuetify,
  router,
  Store,
  components:{
    "app-component":AppComponent,
  }
});

这是我的 store.js

import Vue from 'vue'
import Vuex from  'vuex'
Vue.use(Vuex)

const departmentRoute= [
  { icon: 'mdi-account-badge-outline', text: 'مدیریت  مطالبات   ' ,link:'demands'},
  { icon: 'mdi-account-badge-outline', text: 'مدیریت  گزارشات   ' ,link:'reports'},
];
const SuperAdminRoute= [
  { icon: 'mdi-account-group-outline', text: '  مدیریت کاربران ' ,link:'users'},
  { icon: 'mdi-account-badge-outline', text: 'مدیریت  مطالبات   ' ,link:'demands'},
  { icon: 'mdi-account-badge-outline', text: 'مدیریت  گزارشات   ' ,link:'reports'},
  { icon: 'mdi-account-badge-outline', text: 'آمار و ارقام     ' ,link:'demands'},
];
const studentRoute= [
  { icon: 'mdi-account-group-outline', text: 'آخرین مطالبات به رای گذاشته شده' ,link:'selfDemand'},
  { icon: 'mdi-account-group-outline', text: 'مطالبات من  ' ,link:'selfDemand'},
  { icon: 'mdi-account-badge-outline', text: ' پیگیری مطالبه   ' ,link:'addReport'},
  { icon: 'mdi-checkbox-marked-circle-outline', text: 'تایید حساب کاربری' ,link:'verify'},
];
export default new Vuex.Store({
  state: {
    level: localStorage.getItem('role')
  },
  getters: {
    items: state => {
      switch (state.level) {
        case "super-admin":
          return SuperAdminRoute;
        case "department":
          return departmentRoute;
        default:
          return studentRoute;
      }
    }
  }
})

这是我的应用组件脚本:

<script>
import { mapGetters } from 'vuex'

export default {
  name: "MainDashboard",
  props: {
      source: String,
  },
  computed: {
    ...mapGetters(['items'])
  },
  data: () => ({
    drawer: null,
    title:'فاد | فارغ التحصیلی ناد',
  }),
  methods:{
    logout() {
      axios.post('/api/logout').then(res=>{
        localStorage.removeItem('token');
        localStorage.removeItem('role');
        this.$router.push('/login');
      }).catch(err=>{
      });
    }
  }
}
</script>

当我在我的组件中使用地图获取器时,我可以在 vuex 选项卡中获取我的获取器,但我无法在组件计算属性中获取它! 为什么 ? 我该如何解决?这个错误可能是由于无效的 import vuex 造成的吗?

Vuex 选项卡:

组件:

【问题讨论】:

    标签: vue.js vuex


    【解决方案1】:

    您必须像这样在 app.js 或 main.js 中使用 store:

    import Vue from "vue";
    import App from "./App.vue";
    import store from "./store";
    
    Vue.config.productionTip = false;
    
    new Vue({
      render: (h) => h(App),
      store
    }).$mount("#app");
    

    如果你像 import {store} from "./store"; 这样使用你会得到这个错误。

    【讨论】:

    • 请显示您的使用代码计算了整个 html??
    【解决方案2】:

    我从

    更改了这一行
    import Store from  "./store"
    

    到:

    import store from  "./store"
    

    我的问题解决了!

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 2018-07-14
      • 2021-03-21
      • 2020-09-17
      • 2020-01-01
      • 1970-01-01
      • 2020-04-07
      • 2018-10-01
      • 2019-12-01
      相关资源
      最近更新 更多