seho20001123
seho20001123
  • 发布:2019-04-27 09:50
  • 更新:2019-04-29 10:42
  • 阅读:2496

【uniapp关于vuex的问题】使用vuex的命名空间有页面不渲染的问题

分类:uni-app

首先真机测试是这个问题:
TypeError: Cannot read property 'state' of undefined at D:/%E5%89%8D%E7%AB%AF%E9%A1%B9%E7%9B%AE/youqing_App/node_modules/@dcloudio/vue-cli-plugin-uni/packages/mpvue/index.js:638

表示state找不到,所以看了官方的demo,发现跟我的这个差不多,只是没有用这个namespace命名空间,这个是问题代码;

// vuex  
import { createNamespacedHelpers } from "vuex"; // 取对应的模块  
const { mapState, mapMutations } = createNamespacedHelpers("index");  
// api  
import {  
  userReverse,  
  getViewAllDay,  
  getYesNowTime,  
  getDeskNumber  
} from "@/api/index";  

export default {  
  components: {  
    commonHeader,  
    commonCustomer,  
    styleHead  
  },  
  onShow() {},  
  computed: {  
    ...mapState([  
      "mealId",  
      "deviceContext",  
      "deviceTotal",  
      "choiseCutomer",  
      "confirm_type",  
      "money",  
      "pay_type",  
      "recive_num",  
      "contract_num",  
      "contract_image",  
      "undertake_type",  
      "undertake_time",  
      "undertake_id"  
    ])

vuex代码是这样写的;
store->index.js

/**

  • index首页下的存储仓库
    */
    export default {
    namespaced: true,
    state: {
    // 销售选择客户的信息(id,名称,电话等)
    choiseCutomer: {},
    // 选择菜单的ID
    mealId: 1,
    // 选择菜单的备注
    mealText: "",
    // 设备的json字符串,设备id:设备数量
    deviceContext: "",
    // 设备的总额
    deviceTotal: "",
    undertake_id: "", // 如果合作方式为自营必填项
    // 承办方式
    undertake_type: 0,
    undertake_time: "", // 自带/自营 入场时间必填,
    // 以下为确认方式
    confirm_type: "",
    money: "",
    pay_type: "",
    recive_num: "", // 收款码(确认方式为现金必填)
    contract_num: "", // 合同编码(确认方式为合同必填)
    contract_image: "", // 合同字符串(确认方式为合同必填)
    // 没有看错,没有担保人的选项
    },
    mutations: {
    // 清空所有
    deleteAll(state) {
    state.mealId = 1;
    state.mealText = "";
    state.deviceContext = "";
    state.deviceTotal = "";
    state.undertake_id = "";
    state.undertake_type = "";
    state.undertake_time = "";
    state.confirm_type = "";
    state.money = "";
    state.pay_type = "";
    state.recive_num = "";
    state.contract_num = "";
    state.contract_image = "";
    },
    // 保存选择客户信息
    saveCustomer(state, info) {
    state.choiseCutomer.id = info.id;
    state.choiseCutomer.name = info.name;
    state.choiseCutomer.tel = info.tel;
    },
    // 确认方式保存
    saveSuretype(state, info) {
    state.money = info.money;
    state.pay_type = info.pay_type;
    state.recive_num = info.recive_num;
    state.contract_image = info.contract_image;
    state.contract_num = info.contract_num;
    state.confirm_type = info.confirm_type;
    },
    // 承办方式保存
    saveUnder(state, info) {
    state.undertake_id = info.undertake_id;
    state.undertake_type = info.undertake_type;
    state.undertake_time = info.undertake_time;
    },
    // 把设备明细和设备总额保存进去
    saveDevice(state, info) {
    state.deviceContext = info.deviceContext;
    state.deviceTotal = info.deviceTotal;
    },
    // 保存菜单把ID保存进去
    saveMeal(state, info) {
    state.mealId = info.mealId;
    state.mealText = info.mealText;
    }
    },
    actions: {
    saveMeal(context, mealId) {
    context.commit("saveMeal", mealId);
    }
    }
    }

求官方人员复现代码,看看编译会不会出现问题;

2019-04-27 09:50 负责人:无 分享
已邀请:
seho20001123

seho20001123 (作者) - 菜鸟一个

项目是最新版本的;
用cli搭建的;

  • ZGH0709

    可以找到,bug 未复现,有 demo 吗?

    2019-04-28 12:29

  • seho20001123 (作者)

    回复 ZGH0709: 这个报错什么意思呢?我想请问一下,项目相关代码明早发到你的邮箱

    2019-04-28 23:55

  • ZGH0709

    回复 seho20001123: state undefined, 有可能是其他地方造成的,你把 demo 发上来

    2019-04-29 09:57

  • seho20001123 (作者)

    回复 ZGH0709: 我把具体的代码给到您,您的邮箱是??

    2019-04-29 10:23

  • ZGH0709

    回复 seho20001123: 就直接贴上面就可以,应该可以设置仅管理员可见

    2019-04-29 10:25

  • seho20001123 (作者)

    回复 ZGH0709: 帮忙看一下

    2019-04-29 13:52

seho20001123

seho20001123 (作者) - 菜鸟一个

页面在:/pages/index/index-goReserve.vue

  • ZGH0709

    没问题啊,你在 load 时打印一下,或者在 mapState 中写方法,打印一下看看state, $store 是什么

    2019-04-29 16:54

  • seho20001123 (作者)

    回复 ZGH0709: 这个是在真机测试的时候出现问题的,h5端没问题

    2019-04-29 17:42

  • ZGH0709

    回复 seho20001123: 是的,你打印的结果是什么?

    2019-04-29 18:38

  • seho20001123 (作者)

    回复 ZGH0709: 我明天再测试一下,到时候有问题,我再找您

    2019-04-29 23:51

该问题目前已经被锁定, 无法添加新回复