[TOC]
>[success] # Vuex -- 參數使用理解

~~~
1.'Store' -- vuex 的倉庫儲存這我們需要使用的參數內容在創建的時候使用:
new Vuex.Store({})
2.'state' -- 可以理解成Vue中的data,我們的一些公共數據放在這里
3.'mutations' -- 可以理解成Vue中的methods。也可以理解成java的set。簡單的說
所有對'state' 中的數據更改都是通過'mutations' 中的方法操控,Vuex 不提倡直接
更改'state' 中的數據
4.'getters' -- 可以理解成Vue的computed 計算屬性,或者java中的get,當我們要
獲取'state' 中的方法的時候從getter中取值
5.'Action' -- 異步獲取請求參數賦值,他會操控'mutations',再讓'mutations'給
'state' 賦值
6.'module' --
~~~
>[danger] 全套寫法
>[info] ## 簡單案例 -- 只分析state
~~~
1.下面的案例只是演示如何最簡單的展示'state' 中的數據 實際開發中是不會這么
用的
~~~
>[danger] ##### store文件下的state.js中內容
~~~
1.state 相當于vue中的data 我們在里面創建了一個'appName' data
~~~
~~~
const state= {
appName:'admin',
};
export default state
~~~
>[danger] ##### 在views文件夾下創建視圖組件store.vue
~~~
1.由于我們在mian.js 中的vue.js 實例中使用了'store',因此在全局的組件中我們都
可以調用這個Vuex創建出來的實例,調用的時候使用vue慣有的$調用即可,下
面的案例就是展示了在'store.vue' 視圖中,如何直接使用Vuex 中存的數據展示
2.下面的方式 我們不推薦,但是可以看出使用方法是'this.$store.state.存值的名稱'
3.也可以發現使用的時候我們在計算屬性中定一個了一個方法,作為用來接受
vuex中對應值
~~~
~~~
<template>
<div>
<p>{{appName}}</p>
</div>
</template>
<script>
export default {
name: "store",
data(){
return{}
},
methods:{},
computed:{
// 在計算屬性中 創建一個方法用來接受 vuex 中的stat
appName(){
return this.$store.state.appName;
}
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## 簡單案例-- 如何獲取模塊中的state
~~~
1.我們在創建'store' 文件夾的時候,為了做進一步的拆分,我么拆分出來一個
module 文件夾,這里面存放了一個user 模塊,下面的案例展示展示了如何獲取
模塊中的vuex
~~~
>[danger] ##### 在stor文件下的module文件下的user.js寫法
~~~
const state = {
userName: 'Wang'
};
const mutations = {
};
const actions = {
};
export default {
state,
mutations,
actions,
}
~~~
>[danger] ##### 在views文件夾下創建視圖組件store.vue
~~~
1.在計算屬性中 創建一個方法用來接受 vuex 中的state,掉用的時候
'this.$store.state.模塊名.模塊中'
~~~
~~~
<template>
<div>
<p>{{userName}}</p>
</div>
</template>
<script>
export default {
name: "store",
data(){
return{}
},
methods:{},
computed:{
// 在計算屬性中 創建一個方法用來接受 vuex 中的stat
userName(){
return this.$store.state.user.userName;
}
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## getters -- 頁面獲取值
~~~
1.文章最開始說過不推薦直接使用'state' 并且將內容展示和更改,因此當我們想使
用'state' 的值的時候。我們需要使用vuex中的'getters'。'getters' 相當于vue中的計
算屬性。它是一個方法監聽方法內部調用的state 值發生改變,如果改變就做出對
應的操作
2.根據上面的描述可以勾勒出getters內部是一個個方法,這個方法可以使用到
state中要監聽的具體值,因此寫法如下:
const getters = {
// state 就是vuex 倉儲中的state
// getters 有點類似vue中的計算屬性來監聽內部使用的state值的變化
// 根據state 對應值變化顯示對應信息
appNameWithVersion:(state) =>{
return `${state.appName}v2.0`
}
};
export default getters
~~~
>[danger] ##### store文件下的getters.js中內容
~~~
const getters = {
// state 就是vuex 倉儲中的state
// getters 有點類似vue中的計算屬性來監聽內部使用的state值的變化
// 根據state 對應值變化顯示對應信息
appNameWithVersion:(state) =>{
return `${state.appName}v2.0`
}
};
export default getters
~~~
>[danger] ##### 在views文件夾下創建視圖組件store.vue
~~~
1.使用步驟和'state' ,使用起來'this.$store.getters.對應的getters值'
~~~
~~~
<template>
<div>
<p>{{appName}}</p>
<p>{{userName}}</p>
</div>
</template>
<script>
import { mapState } from 'vuex'
import state from "../store/state";
export default {
name: "store",
data(){
return{}
},
methods:{},
computed:{
// 在計算屬性中 創建一個方法用來接受 vuex 中的getter
appName(){
return this.$store.getters.appNameWithVersion;
},
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## mutations -- 操控值
~~~
1.當我們想改變'state' 中的值的時候可以使用'mutations ',它相當于vue的method
,也就是是可以理解我們來觸發Vuex 中的'mutations '方法來改變getters值
2.每個'mutations ' 中定義的參數兩個值,第一個值是Vuex中的'state' 對象,第二
個是,傳遞過來的參數,也是一個對象
3.如何使用,在Vuex 的'mutations ' 定義好之后,在對應的組件中的methods 方
法去觸發,有點類似子傳父,使用方法:
this.$store.commit('mutations 中的方法名','參數對象')
~~~
>[danger] ##### store文件下的getters.js中內容
~~~
const mutations= {
SET_APP_NAME(state,params){
state.appName = params
},
};
export default mutations
~~~
>[danger] ##### 在views文件夾下創建視圖組件store.vue
~~~
<template>
<div>
<p>{{appNameWithVersion}}</p>
<button @click="handleChangeAppVersion">點擊</button>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
import state from "../store/state";
export default {
name: "store",
data(){
return{}
},
methods:{
handleChangeAppVersion(){
this.$store.commit('SET_APP_NAME','版本')
},
},
computed:{
...mapGetters(['appNameWithVersion'])
}
}
</script>
<style scoped>
</style>
~~~
>[danger] ##### 另一種用法
~~~
1.上面在 this.$store.commit('mutations 中的方法名','參數對象') 傳遞的是兩個參
數,下面這種傳遞是一個對形象
~~~
~~~
const mutations= {
SET_APP_NAME(state,params){
state.appName = params.appName
},
};
export default mutations
~~~
~~~
<template>
<div>
<p>{{appNameWithVersion}}</p>
<button @click="handleChangeAppVersion">點擊</button>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
import state from "../store/state";
export default {
name: "store",
data(){
return{}
},
methods:{
handleChangeAppVersion(){
this.$store.commit({
type:'SET_APP_NAME',
appName:'版本'
})
},
},
computed:{
...mapGetters(['appNameWithVersion'])
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## 思考如果沒在'state' 定義那么'mutations '怎么使用
~~~
Mutation 需遵守 Vue 的響應規則,最好提前在你的 store 中初始化好所有所需屬性。
當需要在對象上添加新屬性時,你應該
1.Vue.set(obj, 'newProp', 123)
2.以新對象替換老對象。例如,利用 stage-3 的對象展開運算符我們可以這樣寫:
state.obj = { ...state.obj, newProp: 123 }
~~~
>[danger] ##### 舉個例子
~~~
1我們沒有在state中定義time,但是在mutations 中應該怎么使用呢
2.注意要是沒用定義的值想在組件中使用需要定義'mapState '
~~~
~~~
import vue from 'vue'
const mutations= {
SET_APP_NAME(state,params){
state.appName = params.appName
},
SET_Time(state,params){
vue.set(state,'timee','17:00')
}
};
export default mutations
~~~
~~~
<template>
<div>
<p>{{appNameWithVersion}}</p>
<p>{{timee}}</p>
<button @click="handleChangeAppVersion">點擊</button>
<button @click="handleChangetime">時間</button>
</div>
</template>
<script>
import { mapGetters,mapState } from 'vuex'
import state from "../store/state";
export default {
name: "store",
data(){
return{}
},
methods:{
handleChangeAppVersion(){
this.$store.commit({
type:'SET_APP_NAME',
appName:'版本'
})
},
handleChangetime(){
this.$store.commit({
type:'SET_Time',
})
},
},
computed:{
...mapState(['timee']),
...mapGetters(['appNameWithVersion',])
}
}
</script>
<style scoped>
</style>
~~~
- Vue--基礎篇章
- Vue -- 介紹
- Vue -- MVVM
- Vue -- 創建Vue實例
- Vue -- 模板語法
- Vue -- 指令用法
- v-cloak -- 遮蓋
- v-bind -- 標簽屬性動態綁定
- v-on -- 綁定事件
- v-model -- 雙向數據綁定
- v-for -- 只是循環沒那么簡單
- 小知識點 -- 計劃內屬性
- key -- 屬性為什么要加
- 案例說明
- v-if/v-show -- 顯示隱藏
- v-for 和 v-if 同時使用
- v-pre -- 不渲染大大胡語法
- v-once -- 只渲染一次
- Vue -- class和style綁定
- Vue -- filter 過濾器
- Vue--watch/computed/fun
- watch -- 巧妙利用watch思想
- Vue -- 自定義指令
- Vue -- $方法
- Vue--生命周期
- Vue -- 專屬ajax
- Vue -- transition過渡動畫
- 前面章節的案例
- 案例 -- 跑馬燈效果
- 案例 -- 選項卡內容切換
- 案例-- 篩選商品
- 案例 -- 搜索/刪除/更改
- 案例 -- 用computed做多選
- 案例 -- checked 多選
- Vue--組件篇章
- component -- 介紹
- component -- 使用全局組件
- component -- 使用局部組件
- component -- 組件深入
- component -- 組件傳值父傳子
- component -- 組件傳值子傳父
- component -- 子傳父語法糖拆解
- component -- 父組件操作子組件
- component -- is 動態切換組件
- component -- 用v-if/v-show控制子組件
- component -- 組件切換的動畫效果
- component -- slot 插槽
- component -- 插槽2.6
- component -- 組件的生命周期
- component -- 基礎組件全局注冊
- VueRouter--獲取路由參數
- VueRouter -- 介紹路由
- VueRouter -- 安裝
- VueRouter -- 使用
- VueRouter--router-link簡單參數
- VueRouter--router-link樣式問題
- VueRouter--router-view動畫效果
- VueRouter -- 匹配優先級
- vueRouter -- 動態路由
- VueRouter -- 命名路由
- VueRouter -- 命名視圖
- VueRouter--$router 獲取函數
- VueRouter--$route獲取參數
- VueRouter--路由嵌套
- VueRouter -- 導航守衛
- VueRouter -- 寫在最后
- Vue--模塊化方式結構
- webpack--自定義配置
- webpack -- 自定義Vue操作
- VueCli -- 3.0可視化配置
- VueCli -- 3.0 項目目錄
- Vue -- 組件升級篇
- Vue -- 組件種類與組件組成
- Vue -- 組件prop、event、slot 技巧
- Vue -- 組件通信(一)
- Vue -- 組件通信(二)
- Vue -- 組件通信(三)
- Vue -- 組件通信(四)
- Vue -- 組件通信(五)
- Vue -- 組件通信(六)
- Vue -- bus非父子組件通信
- Vue -- 封裝js插件成vue組件
- vue組件分裝 -- 進階篇
- Vue -- 組件封裝splitpane(分割面板)
- UI -- 正式封裝
- Vue -- iview 可編輯表格案例
- Ui -- iview 可以同時編輯多行
- Vue -- 了解遞歸組件
- UI -- 正式使用遞歸菜單
- Vue -- iview Tree組件
- Vue -- 利用通信仿寫一個form驗證
- Vue -- 使用自己的Form
- Vue -- Checkbox 組件
- Vue -- CheckboxGroup.vue
- Vue -- Alert 組件
- Vue -- 手動掛載組件
- Vue -- Alert開始封裝
- Vue -- 動態表單組件
- Vue -- Vuex組件的狀態管理
- Vuex -- 參數使用理解
- Vuex -- state擴展
- Vuex -- getters擴展
- Vuex--mutations擴展
- Vuex -- Action 異步
- Vuex -- plugins插件
- Vuex -- v-model寫法
- Vuex -- 更多
- VueCli -- 技巧總結篇
- CLI -- 路由基礎
- CLI -- 路由升級篇
- CLI --異步axios
- axios -- 封裝axios
- CLI -- 登錄寫法
- CLI -- 權限
- CLI -- 簡單權限
- CLI -- 動態路由加載
- CLI -- 數據性能優化
- ES6 -- 類的概念
- ES6類 -- 基礎
- ES6 -- 繼承
- ES6 -- 工作實戰用類數據管理
- JS -- 適配器模式
- ES7 -- 裝飾器(Decorator)
- 裝飾器 -- 裝飾器修飾類
- 裝飾器--修飾類方法(知識擴展)
- 裝飾器 -- 裝飾器修飾類中的方法
- 裝飾器 -- 執行順序
- Reflect -- es6 自帶版本
- Reflect -- reflect-metadata 版本
- 實戰 -- 驗證篇章(基礎)
- 驗證篇章 -- 搭建和目錄
- 驗證篇章 -- 創建基本模板
- 驗證篇章 -- 使用
- 實戰 -- 更新模型(為了迎合ui升級)
- 實戰 -- 模型與接口對接
- TypeSprict -- 基礎篇章
- TS-- 搭建(一)webpack版本
- TS -- 搭建(二)直接使用
- TS -- 基礎類型
- TS -- 枚舉類型
- TS -- Symbol
- TS -- interface 接口
- TS -- 函數
- TS -- 泛型
- TS -- 類
- TS -- 類型推論和兼容
- TS -- 高級類型(一)
- TS -- 高級類型(二)
- TS -- 關于模塊解析
- TS -- 聲明合并
- TS -- 混入
- Vue -- TS項目模擬
- TS -- vue和以前代碼對比
- TS -- vue簡單案例上手
- Vue -- 簡單弄懂VueRouter過程
- VueRouter -- 實現簡單Router
- Vue-- 原理2.x源碼簡單理解
- 了解 -- 簡單的響應式工作原理
- 準備工作 -- 了解發布訂閱和觀察者模式
- 了解 -- 響應式工作原理(一)
- 了解 -- 響應式工作原理(二)
- 手寫 -- 簡單的vue數據響應(一)
- 手寫 -- 簡單的vue數據響應(二)
- 模板引擎可以做的
- 了解 -- 虛擬DOM
- 虛擬dom -- 使用Snabbdom
- 閱讀 -- Snabbdom
- 分析snabbdom源碼 -- h函數
- 分析snabbdom -- init 方法
- init 方法 -- patch方法分析(一)
- init 方法 -- patch方法分析(二)
- init方法 -- patch方法分析(三)
- 手寫 -- 簡單的虛擬dom渲染
- 函數表達解析 - h 和 create-element
- dom操作 -- patch.js
- Vue -- 完成一個minVue
- minVue -- 打包入口
- Vue -- new實例做了什么
- Vue -- $mount 模板編譯階段
- 模板編譯 -- 分析入口
- 模板編譯 -- 分析模板轉譯
- Vue -- mountComponent 掛載階段
- 掛載階段 -- vm._render()
- 掛載階段 -- vnode
- 備份章節
- Vue -- Nuxt.js
- Vue3 -- 學習
- Vue3.x --基本功能快速預覽
- Vue3.x -- createApp
- Vue3.x -- 生命周期
- Vue3.x -- 組件
- vue3.x -- 異步組件???
- vue3.x -- Teleport???
- vue3.x -- 動畫章節 ??
- vue3.x -- 自定義指令 ???
- 深入響應性原理 ???
- vue3.x -- Option API VS Composition API
- Vue3.x -- 使用set up
- Vue3.x -- 響應性API
- 其他 Api 使用
- 計算屬性和監聽屬性
- 生命周期
- 小的案例(一)
- 小的案例(二)-- 泛型
- Vue2.x => Vue3.x 導讀
- v-for 中的 Ref 數組 -- 非兼容
- 異步組件
- attribute 強制行為 -- 非兼容
- $attrs 包括 class & style -- 非兼容
- $children -- 移除
- 自定義指令 -- 非兼容
- 自定義元素交互 -- 非兼容
- Data選項 -- 非兼容
- emits Option -- 新增
- 事件 API -- 非兼容
- 過濾器 -- 移除
- 片段 -- 新增
- 函數式組件 -- 非兼容
- 全局 API -- 非兼容
- 全局 API Treeshaking -- 非兼容
- 內聯模板 Attribute -- 非兼容
- key attribute -- 非兼容
- 按鍵修飾符 -- 非兼容
- 移除 $listeners 和 v-on.native -- 非兼容
- 在 prop 的默認函數中訪問 this -- ??
- 組件使用 v-model -- 非兼容
- 渲染函數 API -- ??
- Slot 統一 ??
- 過渡的 class 名更改 ???
- Transition Group 根元素 -- ??
- v-if 與 v-for 的優先級對比 -- 非兼容
- v-bind 合并行為 非兼容
- 監聽數組 -- 非兼容