uniapp中使用vuex的過程(解決uniapp無法在data和template中獲取vuex數(shù)據(jù)問題)
uniapp中使用vuex(解決uniapp無法在data和template中獲取vuex數(shù)據(jù)問題)
1. uniapp中引入vuex
1 .在根目錄下新建文件夾store,在此目錄下新建index.js文件(uniapp中有自帶vuex插件,直接引用即可)

其中index.js內(nèi)容為
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
hasLogin: false, // 登錄狀態(tài)
userInfo: {}, // 用戶信息
},
mutations: {
setHasLogin(state, value){
state.hasLogin = value
console.log(state.hasLogin)
}
},
actions: {
setHasLogin(context) {
context.commit('setHasLogin')
}
},
getters: {
reverseLoginStatus(state) {
return state.hasLogin = !state.hasLogin
}
}
})在main.js中導(dǎo)入
import Vue from 'vue'
import App from './App'
//這里
import store from '@/store/index.js'
Vue.config.productionTip = false
//這里
Vue.prototype.$store = store
App.mpType = 'app'
const app = new Vue({
...App,
//這里
store,
})
app.$mount()2. uniapp中使用vuex
2.1 this.$store直接操作
//獲取state中的值
this.$store.state.loginStatus
//修改state中的值,這里需要在mutations中定義修改的方法,如上setHasLogin
this.$store.commit('setHasLogin', true);
//調(diào)用actions中的方法,這里需要在actions中定義方法
this.$store.dispatch('setHasLogin')
//調(diào)用getters中的方法,這里需要在getters中定義方法
this.$store.getters.reverseLoginStatus2.2 通過mapState, mapGetters, mapActions, mapMutations
//頁面內(nèi)導(dǎo)入vuex的mapState跟mapMutations方法
import { mapState, mapMutations } from 'vuex'
computed: {
...mapState(['hasLogin'])
}
methods: {
...mapMutations(['setHasLogin']),
}3. 解決uniapp無法在data和template中獲取vuex數(shù)據(jù)問題
需要注意的是,原生vuex用多了很容易順手,this.$store.state直接就用,這里直接寫在dom里是獲取不到的。
//直接在temmplate中使用是無法獲取到的
<temmplate>
<div>{{this.$store.state.loginStatus}}</div>
</temmplate>解決辦法(如上述2.2的使用):
//這樣就可以使用啦
<temmplate>
<div>{{this.$store.state.loginStatus}}</div>
</temmplate>
<script>
export default {
computed:{
loginStatus() {
return this.$store.state.loginStatus
},
}
}
</script>到此這篇關(guān)于uniapp中使用vuex(解決uniapp無法在data和template中獲取vuex數(shù)據(jù)問題)的文章就介紹到這了,更多相關(guān)uniapp使用vuex內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 連接數(shù)據(jù)庫如何操作數(shù)據(jù)庫中的數(shù)據(jù)
JS中怎么連接數(shù)據(jù)庫,和提取數(shù)據(jù)庫中的數(shù)據(jù),本文將以此問題詳細(xì)介紹,需要的朋友可以了解下2012-11-11
JavaScript實(shí)現(xiàn)通過select標(biāo)簽跳轉(zhuǎn)網(wǎng)頁的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)通過select標(biāo)簽跳轉(zhuǎn)網(wǎng)頁的方法,涉及javascript事件響應(yīng)及窗口操作相關(guān)技巧,需要的朋友可以參考下2016-09-09
uni-app常用的幾種頁面跳轉(zhuǎn)方式總結(jié)
uni-app的頁面跳轉(zhuǎn)和小程序和vue很相似,只是方法和標(biāo)簽有所不同,這篇文章主要給大家介紹了關(guān)于uni-app常用的幾種頁面跳轉(zhuǎn)方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
JavaScript立即執(zhí)行函數(shù)IIFE的用法詳解
在JavaScript開發(fā)中,立即執(zhí)行函數(shù)(Immediately Invoked Function Expression,簡稱IIFE)是一種非常實(shí)用的設(shè)計(jì)模式,本文將深入探討IIFE的基本概念、作用以及實(shí)際應(yīng)用場景,并通過多個示例幫助開發(fā)者更好地掌握這一技術(shù),需要的朋友可以參考下2025-02-02
javascript中使用replaceAll()函數(shù)實(shí)現(xiàn)字符替換的方法
第一次發(fā)現(xiàn)JavaScript中replace()?方法如果直接用str.replace("-","!")?只會替換第一個匹配的字符.2010-12-12
JavaScript設(shè)計(jì)模式之單例模式簡單實(shí)例教程
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之單例模式,結(jié)合簡單實(shí)例形式分析了單例模式的概念、功能及javascript定義與使用單例模式相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
使用純前端JavaScript實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出方法過程詳解
這篇文章主要介紹了使用純前端JavaScript實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出方法過程詳解,文章通過示例代碼和圖文解析介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
js bind 函數(shù) 使用閉包保存執(zhí)行上下文
在javascript中,函數(shù)總是在一個特殊的上下文執(zhí)行(稱為執(zhí)行上下文),如果你將一個對象的函數(shù)賦值給另外一個變量的話,這個函數(shù)的執(zhí)行上下文就變?yōu)檫@個變量的上下文了。下面的一個例子能很好的說明這個問題2011-12-12

