>[success] # composition 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] ## 綜合案例
~~~
// 創建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;
~~~
>[danger] ##### 特殊說明 -- state
1. `composition Api` 使用方式比較多,但推薦直接使用 `toRefs` 即可
2. 想使用`mapState` 這類 由于其映射出來的對象,所有要獲取指定`key`對應的`function`,但要注意 `setup` 此時`this ` 指向問題,你需要手動指定`this`,否則執行失敗
* 執行效果圖

~~~html
<template>
<div>
{{ store.state.name }}
{{ name }}
{{ cname }}
{{ tAname }}
{{ tBname }}
{{ rName }}
</div>
<button @click="changeState">changeState</button>
</template>
<script setup>
import { computed, toRefs } from "vue";
import { useStore, mapState } from "vuex";
const store = useStore();
// 方法一 獲取 state 依次賦值
const name = store.state.name; // 非響應
// 方法二 使用計算屬性
const cname = computed(() => store.state.name);
// 方法三使用 mapState 但需要自定義this 指向
const tAname = mapState(["name"]).name.apply({ $store: store }); // 非響應
const tBname = computed(mapState(["name"]).name.bind({ $store: store }));
console.log(mapState(["name"]));
// -----------最簡單的方法 toRefs 解構--------------
const { name: rName } = toRefs(store.state);
// 強制改變state
function changeState() {
store.state.name = "新";
}
</script>
~~~
* 如果非要使用 `mapState `執行可以封裝一個`hooks`
~~~
import { computed } from 'vue'
import { useStore, mapState } from 'vuex'
export default function useState(mapper) {
const store = useStore()
const stateFnsObj = mapState(mapper)
const newState = {}
Object.keys(stateFnsObj).forEach(key => {
newState[key] = computed(stateFnsObj[key].bind({ $store: store }))
})
return newState
}
~~~
~~~
// 使用 封裝的 useState,其實和toRefs 一樣
const { name, level } = useState(["name", "level"])
~~~
>[danger] ##### 其他綜合使用
~~~html
<template>
<div>
{{ getName }}
</div>
<button @click="changeName('111')">changeState</button>
<button @click="increment('111')">incrementAction</button>
<button @click="mapActions('111')">mapActions</button>
</template>
<script setup>
import { toRefs } from "vue";
import { useStore, mapMutations, mapActions } from "vuex";
const store = useStore();
const { getName } = toRefs(store.getters);
// 方式 一
const changeName = (name) => store.commit("changeName", name);
// 方式二 手動的映射和綁定
// const mutations = mapMutations(["changeName"]);
// const newMutations = {};
// Object.keys(mutations).forEach((key) => {
// newMutations[key] = mutations[key].bind({ $store: store });
// });
// const { changeName } = newMutations;
// ------------ action ----------
// 1.使用默認的做法
function increment(name) {
store.dispatch("incrementAction", name);
}
// 2.在setup中使用mapActions輔助函數
// const actions = mapActions(["incrementAction", "changeNameAction"])
// const newActions = {}
// Object.keys(actions).forEach(key => {
// newActions[key] = actions[key].bind({ $store: store })
// })
// const { incrementAction, changeNameAction } = newActions
</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 源碼解讀
- 開發感悟
- 練手項目