1. 程式人生 > 其它 >狀態管理庫vuex與生命週期函式

狀態管理庫vuex與生命週期函式

如何配置:

1.下載並引入vuex相關內容 import {createStore } from 'vuex 2.建立狀態管理庫物件 const store=createStore({       }) 3.對外丟擲store物件 export default store; 4.在main.js中將store物件與當前專案相關聯 createApp(App).user(router).user(store).mount('#app') store狀態管理庫物件重要組成部分 state:元件之間共享的資料集合 元件中獲取方法 1.直接獲取:this.$store.state.xxx; 2.使用物件展開運算子的形式獲取  ...mapState([‘xxx’,‘xxx’]) getters:相當於store物件計算屬性(主要用於對state的資料進行過濾 元件中獲取方法 1.直接獲取 this.$store.getters.xxx; 2.使用物件展開運算子mapGetters(['xxx','xxx']) mutations:定義了修改store物件中state資料的同步方法(要修改的資料不是Ajax非同步獲取的) 元件中獲取 1.直接用commit觸發this.$store.commit('xxxx','傳遞的引數') 2.使用物件展開運算子mapMutaions的形式獲取...mapMutations(['xxx','xxx']) actions定義了修改state資料的非同步方法(修改資料是Ajax獲取的非同步資料) 元件中如何觸發 1.直接用dispatch觸發 this.$store.dispatch('xxx','傳遞的引數') 2.使用物件展開運算子mapActions的形式獲取 ...mapActions(['xxx,'xxx']) 生命週期函式
由 vue 框架提供的 內建函式 ,會伴隨著元件的生命週期,自動按次序執行。
beforeCreate(){
      console.log('beforeCreate:vue物件建立成功之前')
  },
  created(){
      console.log('created:vue物件建立成功之後')
  },
  beforeMount(){
      console.log('beforeMount:view與model繫結成功之前')
  },
   Mounted(){
      console.log('Mounted:view與model繫結成功之前')
  },
  beforeUpdata(){
      console.log(
'beforeUpdata:view與model資料更新之前') }, Updataed(){ console.log('Updataed:view與model資料更新之後') }, beforeDestory(){ console.log('beforeDestory:vue物件銷燬之前') }, Destoryed(){ console.log('Destoryed:vue物件銷燬之後') },