Vuex拿到state中數(shù)據(jù)的3種方式與實例剖析
Ⅰ、Vuex 簡介:
1、Vuex 是什么?
答:Vuex
是一個專為 Vue.js
應(yīng)用程序開發(fā)的狀態(tài)管理模式;
而所謂狀態(tài)就是指:組件中所維護(hù)的數(shù)據(jù));
(簡而言之:就是狀態(tài)管理,解決復(fù)雜組件數(shù)據(jù)通信,狀態(tài)共享;)
2、Vuex 的圖例講解:
其一、對 Vue Components 的理解:
Vue Components
是指:一個組件
(如:compA.vue);
其二、對 State 的理解:
State
是指:存放數(shù)據(jù)
的(數(shù)據(jù)最終是展示(render)在組件的模板(視圖)中);
其三、對 Mutations 的理解:
Mutations
是指:用來存放修改方法
的(且是同步的);
且 Vue Components
可以通過 commit
來修改 Mutations
;
其四、對 Actions 的理解:
Actions
是指:用來放異步操作
的(如:ajax 請求);
且 Vue Components
可以通過 dispatch
派發(fā) Action
的異步請求;
同時: Action 可以直接獲取接口: Backend API, 或通過 Commit 來修改 Mutations 從而修改 State 數(shù)據(jù);
3、Vuex 的配置過程:
其一、選擇并下載 Vuex 版本的過程中:
注意:Vue2
是與 Vuex3相匹配
的,而 Vue3
是與 Vuex4 相匹配
的;
其二、打開終端并輸入命令:
npm i vuex@3
Ⅱ、如何引入并使用 Vuex :
1、用 vue-cli
創(chuàng)建項目;
2、在 src
下建一個 store
文件夾并創(chuàng)建 index.js
文件;
其一、建成的文件夾如下所示:
其二、index.js
里面引入的 vuex
的代碼為:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 注意:一定要用 Vue.use() 注冊一下; const store = new Vuex.Store({ /* 此時的 Vuex.Store 就是一個構(gòu)造函數(shù)(即:相當(dāng)于一個實例); */ // 定義狀態(tài)的地方; state: { num: 1, school: { name: 'xuexiqianduan', age: 26 } }, }) export default store // 此時是導(dǎo)出 store 文件,便于掛載;
3、要在 main.js
文件中掛載一下:
import Vue from 'vue' import App from './App.vue' import store from './store' Vue.config.productionTip = false new Vue({ store, /* 掛載到實例完成后,在 vue 項目的任何地方就都可以使用 store */ render: h => h(App), }).$mount('#app')
4、然后在 App.vue
中使用;
Ⅲ、實例剖析在 App.vue 中使用 state 的過程:
1、方式一:通過 $store.state.num
拿到數(shù)據(jù);
其一、 此時的 App.vue
的代碼為:
<template> <div id="app"> <h1>真實用法:展示Vuex中的State</h1> <p>方式一: num: {{ $store.state.num }}</p> <!-- '$store'就是指:拿到已經(jīng)掛載到實例上的 store 下的 index.js 的內(nèi)容; --> </div> </template> <script> export default { computed: { } } </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
其二、頁面的展示效果為:
其三、而此時 index.js
中的 num
的值為:
(即:已成功拿到了 index.js
中的 num
值;)
2、方式二:通過 {{ num }}
拿到數(shù)據(jù);
其一、 此時的 App.vue
的代碼為:
<template> <div id="app"> <h1>真實用法:展示Vuex中的State</h1> <p>方式二: num: {{ num }}</p> </div> </template> <script> export default { computed: { num() { return this.$store.state.num; }, } } </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
其二、頁面的展示效果為:
其三、而此時 index.js
中的 num
的值為:
(即:已成功拿到了 index.js
中的 num
值;)
3、方式三:通過 {{ num }} {{school.name}}
拿到數(shù)據(jù);
其一、 此時的 App.vue
的代碼為:
<template> <div id="app"> <h1>真實用法:展示Vuex中的State</h1> <p>方式三:num: {{ num }} school: {{ school.name }}</p> </div> </template> <script> import {mapState} from 'vuex' export default { computed: { ...mapState(['num','school']), // 該函數(shù)內(nèi)部運行的返回值大致為:{num: () => this.$store.state.num, school: () => this.$store.state.school} } } } </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
其二、頁面的展示效果為:
其三、而此時 index.js
中的 num
的值為:
(即:已成功拿到了 index.js
中的 num
值;)
Ⅳ、小結(jié):
到此這篇關(guān)于Vuex拿到state中數(shù)據(jù)的3種方式與實例剖析的文章就介紹到這了,更多相關(guān)Vuex拿到state數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用Vue-PDF在應(yīng)用中嵌入PDF文檔
在現(xiàn)代Web應(yīng)用中,PDF文檔的使用非常普遍,因為它可以在各種設(shè)備和操作系統(tǒng)上保持一致的外觀和格式,本文我們就來探討一下如何在Vue.js應(yīng)用中使用vue-pdf庫嵌入PDF文檔吧2023-08-08vue+xlsx實現(xiàn)表格的導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了vue+xlsx實現(xiàn)表格的導(dǎo)入導(dǎo)出功能,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11優(yōu)雅的將ElementUI表格變身成樹形表格的方法步驟
這篇文章主要介紹了優(yōu)雅的將ElementUI表格變身成樹形表格的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04解決vue中el-date-picker?type=daterange日期不回顯的問題
這篇文章主要介紹了解決vue中el-date-picker?type=daterange日期不回顯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10element修改form的el-input寬度,el-select寬度的方法實現(xiàn)
有時候像form表單這樣,頁面的input、select等寬度不一定會是一樣的,可能有些長,有些短,本文就介紹了如何element修改form的el-input寬度,el-select寬度的方法實現(xiàn),感興趣的可以了解一下2022-02-02vue3 reactive 請求接口數(shù)據(jù)賦值后拿不到的問題及解決方案
這篇文章主要介紹了vue3 reactive 請求接口數(shù)據(jù)賦值后拿不到的問題及解決方案,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04