_Zora. 2024-07-02 20:54 采纳率: 94.4%
浏览 6
已结题

vuex共享数据渲染页面失败

用vuex来实现数据共享,已经对vuex进行了配置,并且能够在后台输出共享值能够变化,但是无论用插值语法还是计算属性还是直接数据赋值,vue就是检测不到共享数据的变化

第一个代码(index.js也就是配置vuex)

//这个文件用来创建vuex中的核心store
import Vue from 'vue'

//引入vuex
import Vuex from 'vuex'

//准备actions----用来响应组件中的动作,中间商
const actions={
  jia(context,value){
    //context里面放着很多对象,有commit和dispatch等,value就是传进来的数据
    context.commit('JIA',value)
    //这个JIA是用来传给mutations,因为mutations是真正处理数据的
  }
}

// 准备mutations-----用来操作数据(state )
const mutations={
  JIA(state,value){
    //这个state就是流程图的mutations的下一步state
    state.sum+=value
    // console.log(state);
  }
}

//准备state-----用来存储数据
const state={
  sum:0//和
}

//使用插件,有了vuex那么就有了store对象
Vue.use(Vuex)

//暴露store
//创建store
export default new Vuex.Store(
  {
    actions,
    mutations,
    state
  }
)




第二个代码(MainComponent组件,用来修改共享数据)


<template>
  <div class="big_box">
    <h1>当前求和为:{{ this.$store.state.sum}}</h1>
    <select v-model.number="n">
      <option :value="1">1</option>
      <option :value="2">2</option>
      <option :value="3">3</option>
    </select>
    <button @click="add">+</button>
    <button @click="dec">-</button>
    <button @click="add_ood">当前求和为奇数再加</button>
    <button @click="wait">等一等再加</button>
  </div>
</template>

<script>
export default {
  name: 'MainComponent',
  data() {
    return {
      n:1,//用户输入的数字
      watch_data: this.$store.state.sum
    }
  },
  methods: {
   add(){
    this.$store.dispatch('jia',this.n)
    this.watch_data=this.$store.state.sum
    console.log(1111111,);
       },
  //  dec(){
    
  //  },
  //  add_ood(){
  //   if (this.n%2) {
     
  //   }
  //  },
  //  wait(){
  //   setTimeout(()=>{
      
  //   },1000)
  //  }
  },
 computed: {
  watchdata(){
     console.log(this.$store.state.sum);
     return this.$store.state.sum
  }
 },
}
</script>


<style scoped>

button{
  margin-left: 10px;
}
</style>


在MainComponent组件里是为了修改index.js里面的sum值,this.$store.state.sum可以在后台输出数值变化,但是无论用什么方法渲染到网页上就不行,页面如下

img

而且报错
Error in nextTick: "TypeError: Cannot read properties of undefined (reading '_wrapper')",不知道错误是什么,希望得到解答,谢谢各位

  • 写回答

3条回答 默认 最新

  • 雾里桃花 2024-07-03 08:58
    关注

    直接渲染 watchdata 试试

    
    <template>
      <div class="big_box">
        <h1>当前求和为:{{ watchdata }}</h1>
        <select v-model.number="n">
          <option :value="1">1</option>
          <option :value="2">2</option>
          <option :value="3">3</option>
        </select>
        <button @click="add">+</button>
        <button @click="dec">-</button>
        <button @click="add_ood">当前求和为奇数再加</button>
        <button @click="wait">等一等再加</button>
      </div>
    </template>
     
    <script>
    export default {
      name: 'MainComponent',
      data() {
        return {
          n:1,//用户输入的数字
          watch_data: this.$store.state.sum
        }
      },
      methods: {
       add(){
        this.$store.dispatch('jia',this.n)
        this.watch_data=this.$store.state.sum
        console.log(1111111,);
           },
      //  dec(){
        
      //  },
      //  add_ood(){
      //   if (this.n%2) {
         
      //   }
      //  },
      //  wait(){
      //   setTimeout(()=>{
          
      //   },1000)
      //  }
      },
     computed: {
      watchdata(){
         console.log(this.$store.state.sum);
         return this.$store.state.sum
      }
     },
    }
    </script>
     
     
    <style scoped>
     
    button{
      margin-left: 10px;
    }
    </style>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 7月2日

悬赏问题

  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22