vuex中的state使用及說(shuō)明
vuex中的state
本文講解vuex中的state使用方法。
入門講解
首先第一步肯定是創(chuàng)建vue項(xiàng)目,具體操作看這篇文章:用命令窗口的方式創(chuàng)建Vue項(xiàng)目
首先在store文件夾下面,創(chuàng)建一個(gè)state.js文件,存放公共的數(shù)據(jù)
在store文件夾下面的index.js文件中進(jìn)行如下配置。
然后前端可以采取這兩種寫法,就可以訪問(wèn)到存儲(chǔ)的數(shù)據(jù)了。
所以我們可以知道的是這個(gè)state.js就是用來(lái)存放數(shù)據(jù)的。
mapState輔助函數(shù)
具體代碼如下:
<template> <div class="about"> <h1>This is an about page</h1> {{ $store.state.str }} <hr> {{ str }} <hr> {{ a }} <hr> {{ b }} </div> </template> <script> import { mapState } from 'vuex' export default { computed: { ...mapState(['str', 'a', 'b']) } } </script>
運(yùn)行結(jié)果:
案例
好的,在不增加難度的情況下,我來(lái)給您修改一下之前的案例。
案例 1:在線狀態(tài)
- 思路
const store = new Vuex.Store({ state: { onlineStatus: false } })
這里我們定義了一個(gè)名為 onlineStatus
的屬性,并初始化為 false
。
當(dāng)用戶上線時(shí),可以更新 onlineStatus
屬性:
store.state.onlineStatus = true
這將直接更改 onlineStatus
屬性中的值。
然后,你可以通過(guò)其他組件調(diào)用此值:
computed: { onlineStatus() { return this.$store.state.onlineStatus } }
完整代碼
- 項(xiàng)目結(jié)構(gòu)
- state.js
export default { onlineStatus: false }
- index.js
import { createStore } from 'vuex' import state from './state' export default createStore({ state, mutations: { SET_ONLINE_STATUS (state, status) { state.onlineStatus = status } } })
- TestView.vue
<template> <div> <p>Your online status is {{ onlineStatusText }}</p> </div> </template> <script> export default { computed: { onlineStatusText () { return this.$store.state.onlineStatus ? 'Online' : 'Offline' } } } </script> <style> /* 樣式可以選擇添加 */ </style>
案例 2:主題樣式
- 思路
const store = new Vuex.Store({ state: { themeColor: 'blue' } })
我們定義了一個(gè)名為 themeColor
的屬性,并用 "blue"
初始化它。
為了更改主題顏色,可以直接設(shè)置 themeColor
的值:
store.state.themeColor = 'red'
這將直接更改我們的主題顏色值。
然后,你可以通過(guò)其他組件調(diào)用此值:
computed: { themeColor() { return this.$store.state.themeColor } }
完整代碼
- state.js
export default { onlineStatus: false, themeColor: 'blue' }
- index.js
import { createStore } from 'vuex' import state from './state' export default createStore({ state, mutations: { SET_ONLINE_STATUS (state, status) { state.onlineStatus = status }, SET_THEME_COLOR (state, color) { state.themeColor = color } } })
- TestView.vue
<template> <div :style="{ background: themeColor }"> <p>Your theme color is {{ themeColor }}</p> <button @click="changeThemeColor">Change Theme Color</button> </div> </template> <script> export default { computed: { themeColor () { return this.$store.state.themeColor } }, methods: { changeThemeColor () { this.$store.state.themeColor = 'red' } } } </script> <style> /* 樣式可以自定義 */ </style>
運(yùn)行結(jié)果
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果
vant移動(dòng)端顯示table效果,增加復(fù)選框,可以進(jìn)行多選和全選,加橫向滾動(dòng)條,可以看全部?jī)?nèi)容,下面通過(guò)本文給大家分享vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果,感興趣的朋友跟隨小編一起看看吧2024-06-06Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實(shí)現(xiàn)
本文主要介紹了Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Vue2.0+Vux搭建一個(gè)完整的移動(dòng)webApp項(xiàng)目的示例
這篇文章主要介紹了Vue2.0+Vux搭建一個(gè)完整的移動(dòng)webApp項(xiàng)目的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03vue返回上一頁(yè)面時(shí)不刷新問(wèn)題及解決方案
這篇文章主要介紹了vue返回上一頁(yè)面時(shí)不刷新問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05詳解使用vue-admin-template的優(yōu)化歷程
這篇文章主要介紹了詳解使用vue-admin-template的優(yōu)化歷程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Vue實(shí)現(xiàn)移動(dòng)端頁(yè)面切換效果【推薦】
這篇文章主要介紹了Vue實(shí)現(xiàn)移動(dòng)端頁(yè)面切換效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11vue實(shí)現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問(wèn)題(推薦)
最近公司項(xiàng)目需要,利用vue實(shí)現(xiàn)pdf導(dǎo)出,從而保存到本地打印出來(lái),說(shuō)起來(lái)好像也很容易,具體要怎么實(shí)現(xiàn)呢?下面小編給大家?guī)?lái)了vue實(shí)現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問(wèn)題,需要的朋友參考下吧2018-10-10Vue-cli proxyTable 解決開發(fā)環(huán)境的跨域問(wèn)題詳解
本篇文章主要介紹了Vue-cli proxyTable 解決開發(fā)環(huán)境的跨域問(wèn)題詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05Vue實(shí)現(xiàn)二維碼數(shù)組的全選與反選功能
在開發(fā)Web應(yīng)用程序時(shí),表格數(shù)據(jù)的展示和操作是非常常見的需求之一,特別是在處理表格中的復(fù)選框選擇時(shí),我們經(jīng)常需要實(shí)現(xiàn)全選、反選等功能,這篇文章將帶你深入了解如何在Vue.js中實(shí)現(xiàn)對(duì)二維數(shù)組數(shù)據(jù)的全選和反選功能,需要的朋友可以參考下2024-09-09