大前端react & vue & angular

Vuex 状态管理

2022-03-08  本文已影响0人  丽__
一、组件内的状态管理流程
image.png
二、组件间通信方式——父组件给子组件传值
// 父组件
<child title='父组件'></child>

import child from 'child'
export default{
  components:{
    child
  }
}

//子组件
prop:['title']
或
prop:{
  title:String
}
三、组件间通信方式——子组件给父组件传值
//父组件

<child @enlargeText='enlargeText'></child>
<child @enlargeText='hFontSize += $event'></child>

import child from 'child'
export default{
  components:{
    child
  },
  methods:{
    enlargeText(size){
      console.log('子组件传过来的值为:'+size)
    }
  }
}


//子组件
<button @click="handler">点击文字切换大小</button>


handler(){
  this.$emit('enlargeText',20)
}
四、组件间通信方式——不相关组件传值(bus)
// bus.js
import Vue from 'vue'
export detault new Vue()


// 01.vue
<div class="number" @click="sub"> - </div>
<input type="text" :value="value">
<div class="number" @click="add"> + </div>

import bus from 'bus.js'
export default{
  props:{
    num:Number
  },
  created(){
    this.value = this.num
  },
  data(){
    return {
      value:-1
    }
  },
  methods:{
    sub(){
      this.value--;
      bus.$emit('numchange',this.value)
    },
    add(){
      this.value++;
      bus.$emit('numchange',this.value)
    }
  }
}


// 02.vue
<div>{{ msg }}</div>

import bus from 'bus.js'
export default{
  data(){
    return{
       msg:''
    }
  },
  created(){
    bus.$on('numchange',(value)=>{
      this.msg = `您选择了${value}件商品`
    })
  }
}

app.vue
<sibling01 :num='2'><sibling01>
<sibling02></sibling02>
五、组件间通信方式——通过ref获取子组件
this.$refs.refsName
六、Vuex介绍

七、Vuex核心概念

八、Vuex基本结构
image.png
image.png
九、State
import { mapState } from 'vuex'
export default{
  computed:{
     //msg : state=>state.msg
    ...mapState(['msg']);  //写法1
    ...mapState({ message : 'msg' }); //写法2  
  }
}
十、Getter
// vuex.js
getters:{
  reverseMsg(state){
    return state.msg.split('').reverse().join('-')
  }
}
import { mapGetters } from 'vuex'
export default{
  computed:{
    ...mapGetters(['reverseMsg']); 
  }
}
十一、Mutation (不要进行一步操作)
//vuex.js
mutations:{
  increate(state,payload){
    state.count += payload
  }
}
import { mapMutations} from 'vuex'
export default{
  methods:{
    ...mapMutations(['increate']); 
  }
}
十二、Action
// vuex.js
actions:{
  increateAsync(context,payload){
    setTimeout(()=>{
      context.commit('increate',payload)
    },2000)
  }
}

import { mapMutations} from 'vuex'
export default{
  methods:{
    ...mapActions(['increateAsync']); 
  }
}
十三、Module
const state ={}
const getters ={}
const mutations ={}
const actions ={}
export default {
  namespaced:true,//开启命名空间
  state,
  getters,
  mutations,
  actions
}
...mapState('Module模块名字',['msg'])
...mapGetters('Module模块名字',['模块内getters'])
...mapMutations('Module模块名字',['模块内方法名'])
十四、Vuex严格模式
//vuex.js
export default new Vuex.store({
  strict:true, // 开启
  strict:process.env.NODE_ENV!=='production', // 生产环境开启
  state:{},
  getters:{},
  mutations:{},
  actions:{}
})
十五、VUEX插件介绍
const myPlugin = store =>{
  //当store初始化后调用
  store.subscribe((mutation,state) =>{
    //每次mutation之后调用
    //mutation的格式为{ type,payload }
  })
}

const store = new Vuex.Store({
  plugins:[myPl]
})
十六、模拟VUEX
const _Vue = null

class Store {
  constructor (options) {
    const {
      state = {},
      getters = {},
      mutations = {},
      actions = {}
    } = options
    this.state = _Vue.observable(state)
    this.getters = Object.create(null)
    Object.keys(getters).forEach(key => {
      Object.defineProperty(this.getters, key, {
        get: () => getters[key](state)
      })
    })
    this._mutation = mutations
    this._actions = actions
  }

  commit (type, payload) {
    this._mutation[type](this.state, payload)
  }

  dispatch (type, payload) {
    this._actions[type](this, payload)
  }
}

function install (Vue) {
  _Vue = Vue
  _Vue.mixin({
    beforeCreate () {
      if (this.$options.store) {
        _Vue.prototype.$store = this.$options.store
      }
    }
  })
}

export default {
  Store,
  install
}

上一篇下一篇

猜你喜欢

热点阅读