如何使用 vue + typescript 編寫頁面 ( vuex裝飾器部分 )

簡單瞭解一下vuex

是vue提供的一款store,用來存儲頁面共享數據vue

什麼時候須要vuex

通常來講,數據分爲兩種方式ios

  1. 自有數據,即組件自己持有數據,表現即 data部分
  2. 外部數據,可由prop標籤屬性,inject父級注入,vuex提供。

組件自己自身持有數據內容,並不須要外部的參與的狀況下,不須要外部數據。可是在通常來講,使用外部數據比較常見。 prop與父級緊密相關vuex

使用inject注入時,沒法有效追蹤層級時,查找內容提供者容易出錯,一般不建議跨多個層級使用typescript

使用vuex好處

  1. 統一數據來源,方便查找 對標Provide/Inject
  2. 方便數據共享,對標Prop 子-父 傳遞數據

不方便的地方

暫時尚未發現,發現了再補充npm

使用vuex要考慮的地方

使用vuex的目的就是提升數據的複用性,可是不能夠盲目使用vuex。promise

  • 和組件自生無關的數據,能夠放在store裏。
  • 在不一樣頁面須要引用相同數據時,須要放在store裏,以減小網絡請求的次數。
  • 單純的父子傳遞數據,不必定要放在store裏

基礎內容

開打demo的store.ts 文件做爲參照網絡

export default new Vuex.Store({
  state: { /* 狀態庫 */ },
  mutations: { /* 同步計算庫 使用commit觸發 */ },
  actions: { /* 異步計算庫 使用dispatch觸發 */ },
  getters: { /* 計算屬性 */ },
  modules: { /* 分模塊 */
    test: {
      namespaced: true, /* 開啓module模式 */
      state: {
        foo: "this is foo"
      },
      getters: {
        getFoo(state) { 
          return state.foo;
        }
      },
      mutations: {
        setFoo(state, foo) {
          state.foo = foo;
        }
      },
      actions: {
        setFoo({ commit }, foo) {
          setTimeout(() => {
            commit('setFoo',foo)
          }, 1e3);
        }
      }
    }
  }
})
複製代碼

認識vuex的裝飾器 vuex-class

import {
  State,
  Getter,
  Action,
  Mutation,
  namespace
} from 'vuex-class'
複製代碼

1. @namespace 命名空間

在咱們開啓module模式的時候,從模塊下映射須要使用到namespace異步

State外,其他能夠行內書寫方式直接引用,不須要引入 namespaceasync

namespace的兩種使用方式ide

  1. 直接裝飾器方式,能夠簡單使用
@Component
class MyComponent extends Vue{
    @namespace('test').State foo!: string;
}
複製代碼
  1. 引用裝飾器方式,這種方式適合多個須要引用時簡化書寫namespace
const TestModule = namespace("test")

@Component
class MyComponent extends Vue{
    @TestModule.State foo!: string;
}
複製代碼

2. @State 狀態映射

  • 直接映射 @State foo@State("foo") foo 相同
@Component
class MyComponent extends Vue{
    @namespace('test').State foo!: string;
}
複製代碼
  • 獲取計算後的state

state映射到組件時,是放在computed下的,所以自己爲計算屬性。

@Component
class MyComponent extends Vue{
    /* 只從 state 獲取*/
    @namespace('test').State(state=>state.foo) foo!: string;
    /*從state和getters上獲取*/
    @namespace('test').State((state,getters)=>state.foo+'getter:'+getters.getFoo) svsgfoo!: string;
}
複製代碼
  • 內部使用namespace 若是不想在外部使用namespace,可使用參數傳遞namespace
@Component
class MyComponent extends Vue{
    @State("foo",{namespace:"test"}) foo!: string;
}
複製代碼

3. @Getter 計算屬性

和State相似,可是不支持再次計算。

@Component
class MyComponent extends Vue{
   @Getter("test/getFoo") namefoo!:string;
   @Getter("getFoo",{namespace:"test"}) foo!:string;
   @namespace("test").Getter getFoo!:string;
}
複製代碼

4. @Action 異步計算庫

書寫方式和Getter相似

@Component
class MyComponent extends Vue{
   @Action("test/setFoo") setFoo!: Function;
}

複製代碼
  • action能夠配合promise + async/await 一同使用
actions:{
    async queryState({commit},token){
        let result = await Axios.get("url",{data:{ token }})
        commit('state',result.data)
        return result.data
    }
}
複製代碼
@Component
class MyComponent extends Vue{
   private token:string="best";
   @Action queryState!: Function;
   
   async onQueryStateClick(){
       let data = await this.queryState(this.token)
       // todo ...
   }
}
複製代碼

5. @Mutation 同步計算庫

書寫方式和Action相似

@Component
class MyComponent extends Vue{
   @Action("test/setFoo") setFoo!: Function;
}

複製代碼

可是mutation會當即返回結果,所以異步請求應該放在action中

更多內容 :vuex能夠參照這裏,vuex裝飾器具體可參照 vuex-class

上一章:如何使用 vue + typescript 編寫頁面 ( 基礎裝飾器部分 )

相關文章
相關標籤/搜索