>[success] # Option Api -- vuex
1. 可以通過 獲取`this.$store` 來進行調用里面提供數據對象
* **state**-- 保存數據狀態
* **mutations** -- 對`state `中的數據更改都是通過`mutations`中的方法操控,`Vuex `不提倡直接更改`state`中的數據
* **getters** -- 當我們要獲取`state`中的方法的時候從`getter`中取值
* **Action** -- 異步獲取請求參數賦值,他會操控`mutations`,再讓`mutations`給`state`賦值
* **module**-- store 分割成模塊(module)。每個模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊
>[info] ## 綜合案例
1. 正常使用是通過`this.$store` 去調用對應模塊中取出數據
2. `vuex `也提供一些便捷方法**mapGetters, mapState, mapMutations, mapActions**
* 注冊
~~~
// 創建vuex
import { createStore } from "vuex";
const store = createStore({
state: () => {
return {
name: "wwww",
age: 12,
friends: [
{ id: 111, name: "a", age: 20 },
{ id: 112, name: "b", age: 30 },
{ id: 113, name: "c", age: 25 },
],
};
},
getters: {
// 第一個參數當前state
getName(state) {
return state.name;
},
// 第一個參數當前state 第二個參數是getters
getInfo(state, getters) {
return state.age + getters.getName;
},
// 通過返回一個函數達到 讓 getter 可以接收參數
getFriendById(state) {
return function (id) {
const friend = state.friends.find((item) => item.id === id);
return friend;
};
},
},
mutations: {
// 第一個參數 state ,第二個參數調用傳入的值
changeName(state, payload) {
state.name = payload;
},
},
actions: {
/** 處理函數總是接受 context 作為第一個參數,context 對象包含以下屬性
* state, // 等同于 `store.state`,若在模塊中則為局部狀態
* rootState, // 等同于 `store.state`,只存在于模塊中
* commit, // 等同于 `store.commit`
* dispatch, // 等同于 `store.dispatch`
* getters, // 等同于 `store.getters`
* rootGetters // 等同于 `store.getters`,只存在于模塊中
*/
incrementAction(context, payload) {
// console.log(context.commit) // 用于提交mutation
// console.log(context.getters) // getters
// console.log(context.state) // state
context.commit("changeName", payload);
},
},
});
export default store;
~~~
* 使用
~~~html
<template>
<div>{{ getName }} -- {{ getInfo }} -- {{ getFriendById(111) }}</div>
<div>{{ cgetName }}</div>
<button @click="mutations">觸發mutations</button>
<button @click="changeName('語法糖')">觸發mutations 語法糖</button>
<button @click="actions">Actions</button>
<button @click="incrementAction('Actions')">觸發Actions 語法糖</button>
</template>
<script>
import { mapGetters, mapState, mapMutations, mapActions } from "vuex";
export default {
computed: {
...mapState([
// 映射 this.name 為 store.state.name
"name",
]),
...mapState({
age: (state) => state.age,
nameAlias: "name", //傳字符串參數 'nameAlias' 等同于 `state => state.name`
}),
//---------------分割線 ------------
...mapGetters(["getName", "getInfo"]),
...mapGetters(["getFriendById"]),
...mapGetters({
// 把 `this.zgetName` 映射為 `this.$store.getters.getName`
zgetName: "getName",
}),
cgetName() {
return this.$store.getters.getName;
},
},
methods: {
mutations() {
this.$store.commit("changeName", "新名字");
},
...mapMutations(["changeName"]), // `this.changeName(amount)` 映射為 `this.$store.commit('changeName', amount)`
//---------------分割線 ------------
actions() {
this.$store.dispatch("incrementAction", "actions");
},
...mapActions(["incrementAction"]), // `this.incrementAction(amount)` 映射為 `this.$store.dispatch('incrementAction', amount)`
},
};
</script>
~~~
- 官網給的工具
- 聲明vue2 和 vue3
- 指令速覽
- Mustache -- 語法
- v-once -- 只渲染一次
- v-text -- 插入文本
- v-html -- 渲染html
- v-pre -- 顯示原始的Mustache標簽
- v-cloak -- 遮蓋
- v-memo(新)-- 緩存指定值
- v-if/v-show -- 條件渲染
- v-for -- 循環
- v-bind -- 知識
- v-bind -- 修飾符
- v-on -- 點擊事件
- v-model -- 雙向綁定
- 其他基礎知識速覽
- 快速使用
- 常識知識點
- key -- 作用 (后續要更新)
- computed -- 計算屬性
- watch -- 偵聽
- 防抖和節流
- vue3 -- 生命周期
- vue-cli 和 vite 項目搭建方法
- vite -- 導入動態圖片
- 組件
- 單文件組件 -- SFC
- 組件通信 -- porp
- 組件通信 -- $emit
- 組件通信 -- Provide / Inject
- 組件通信 -- 全局事件總線mitt庫
- 插槽 -- slot
- 整體使用案例
- 動態組件 -- is
- keep-alive
- 分包 -- 異步組價
- mixin -- 混入
- v-model-- 組件
- 使用計算屬性
- v-model -- 自定義修飾符
- Suspense -- 實驗屬性
- Teleport -- 指定掛載
- 組件實例 -- $ 屬性
- Option API VS Composition API
- Setup -- 組合API 入口
- api -- reactive
- api -- ref
- 使用ref 和 reactive 場景
- api -- toRefs 和 toRef
- api -- readonly
- 判斷性 -- API
- 功能性 -- API
- api -- computed
- api -- $ref 使用
- api -- 生命周期
- Provide 和 Inject
- watch
- watchEffect
- watch vs. watchEffect
- 簡單使用composition Api
- 響應性語法糖
- css -- 功能
- 修改css -- :deep() 和 var
- Vue3.2 -- 語法
- ts -- vscode 配置
- attrs/emit/props/expose/slots -- 使用
- props -- defineProps
- props -- defineProps Ts
- emit -- defineEmits
- emit -- defineEmits Ts
- $ref -- defineExpose
- slots/attrs -- useSlots() 和 useAttrs()
- 自定義指令
- Vue -- 插件
- Vue2.x 和 Vue3.x 不同點
- $children -- 移除
- v-for 和 ref
- attribute 強制行為
- 按鍵修飾符
- v-if 和 v-for 優先級
- 組件使用 v-model -- 非兼容
- 組件
- h -- 函數
- jsx -- 編寫
- Vue -- Router
- 了解路由和vue搭配
- vueRouter -- 簡單實現
- 安裝即使用
- 路由懶加載
- router-view
- router-link
- 路由匹配規則
- 404 頁面配置
- 路由嵌套
- 路由組件傳參
- 路由重定向和別名
- 路由跳轉方法
- 命名路由
- 命名視圖
- Composition API
- 路由守衛
- 路由元信息
- 路由其他方法 -- 添加/刪除/獲取
- 服務器配置映射
- 其他
- Vuex -- 狀態管理
- Option Api -- VUEX
- composition API -- VUEX
- module -- VUEX
- 刷新后vuex 數據同步
- 小技巧
- Pinia -- 狀態管理
- 開始使用
- pinia -- state
- pinia -- getter
- pinia -- action
- pinia -- 插件 ??
- Vue 源碼解讀
- 開發感悟
- 練手項目