vuex中的state使用及說明
vuex中的state
本文講解vuex中的state使用方法。
入門講解
首先第一步肯定是創(chuàng)建vue項目,具體操作看這篇文章:用命令窗口的方式創(chuàng)建Vue項目
首先在store文件夾下面,創(chuàng)建一個state.js文件,存放公共的數(shù)據(jù)
在store文件夾下面的index.js文件中進行如下配置。
然后前端可以采取這兩種寫法,就可以訪問到存儲的數(shù)據(jù)了。
所以我們可以知道的是這個state.js就是用來存放數(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>
運行結(jié)果:
案例
好的,在不增加難度的情況下,我來給您修改一下之前的案例。
案例 1:在線狀態(tài)
- 思路
const store = new Vuex.Store({ state: { onlineStatus: false } })
這里我們定義了一個名為 onlineStatus
的屬性,并初始化為 false
。
當(dāng)用戶上線時,可以更新 onlineStatus
屬性:
store.state.onlineStatus = true
這將直接更改 onlineStatus
屬性中的值。
然后,你可以通過其他組件調(diào)用此值:
computed: { onlineStatus() { return this.$store.state.onlineStatus } }
完整代碼
- 項目結(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' } })
我們定義了一個名為 themeColor
的屬性,并用 "blue"
初始化它。
為了更改主題顏色,可以直接設(shè)置 themeColor
的值:
store.state.themeColor = 'red'
這將直接更改我們的主題顏色值。
然后,你可以通過其他組件調(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>
運行結(jié)果
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實現(xiàn)
本文主要介紹了Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Vue2.0+Vux搭建一個完整的移動webApp項目的示例
這篇文章主要介紹了Vue2.0+Vux搭建一個完整的移動webApp項目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03詳解使用vue-admin-template的優(yōu)化歷程
這篇文章主要介紹了詳解使用vue-admin-template的優(yōu)化歷程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05vue實現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問題(推薦)
最近公司項目需要,利用vue實現(xiàn)pdf導(dǎo)出,從而保存到本地打印出來,說起來好像也很容易,具體要怎么實現(xiàn)呢?下面小編給大家?guī)砹藇ue實現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問題,需要的朋友參考下吧2018-10-10Vue-cli proxyTable 解決開發(fā)環(huán)境的跨域問題詳解
本篇文章主要介紹了Vue-cli proxyTable 解決開發(fā)環(huán)境的跨域問題詳解,非常具有實用價值,需要的朋友可以參考下2017-05-05