Basic usage of vuex (state sharing)

When the project is created, bring a store folder back to the root directory

Insert picture description here

The data defined in state is shared data, and mutations are the custom method names (the first of the two parameters is the state of the shared data as a whole, and the second value is the value to be changed)

Insert picture description here

Use in js (change the value)
    //得到焦点事件
    get() {
    
    
      //commit传入两个个参数(第一个为vuex内mutations内方法的名字, 第二个为改变之后的值)
      this.$store.commit("setrrr", 0);
    },
Use in html
 <div v-if="$store.state.rrr===0" class="app"></div>
Use in js (rendering data)
  computed: {
    
    
    //通过计算属性拿到,名字不可以和data重合
    num() {
    
    
      return this.$store.state.rrr;
    },
  },
Use in html (rendering data)
<div>{
    
    {
    
    num}}</div>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49866029/article/details/108244339