欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuex中使用modules時(shí)遇到的坑及問(wèn)題

 更新時(shí)間:2022年08月31日 15:08:13   作者:Mortimery  
這篇文章主要介紹了vuex中使用modules時(shí)遇到的坑及問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vuex使用modules時(shí)遇到的坑

其實(shí)也不算坑,只是自己沒(méi)注意看官網(wǎng)api,定義module另外命名時(shí),需要在module中加一個(gè)命名空間namespaced: true

屬性,否則命名無(wú)法暴露出來(lái),導(dǎo)致報(bào)[vuex] module namespace not found in mapState()等錯(cuò)誤。

vuex中modules基本用法

1. store文件結(jié)構(gòu)

- src
- components
- store
? ? -index.js
? ? -modules
? ? ? ? -app.js
? ? ? ? -bus.js

2.1 index.js中-手動(dòng)引入modules

import Vue from 'vue'
import Vuex from 'vuex'
import bus from './module/bus'
import app from './module/app'
Vue.use(Vuex)
export default new Vuex.Store({
? ? state: {
? ? ? ? // 這里是根vuex狀態(tài)
? ? },
? ? mutations: {
? ? ? ? // 這里是根vuex狀態(tài)
? ? },
? ? actions: {
? ? ? ? // 這里是根vuex狀態(tài)
? ? },
? ? modules: { // 子vuex狀態(tài)模塊注冊(cè)
? ? ? ? namespaced: true, // 為了解決不同模塊命名沖突的問(wèn)題
? ? ? ? app,
? ? ? ? bus
? ? }
})

2.2 index.js中-動(dòng)態(tài)引入modules

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const dynamicModules = {}
const files = require.context('.', true, /\.js$/)
const dynamicImportModules = (modules, file, splits, index = 0) => {
? ? if (index == 0 && splits[0] == 'modules') {
? ? ? ? ++index
? ? }
? ? if (splits.length == index + 1) {
? ? ? ? if ('index' == splits[index]) {
? ? ? ? ? ? modules[splits[index - 1]] = files(file).default
? ? ? ? } else {
? ? ? ? ? ? modules.modules[splits[index]] = files(file).default
? ? ? ? }
? ? } else {
? ? ? ? let tmpModules = {}
? ? ? ? if ('index' == splits[index + 1]) {
? ? ? ? ? ? tmpModules = modules
? ? ? ? } else {
? ? ? ? ? ? modules[splits[index]] = modules[splits[index]] ? modules[splits[index]] : {namespaced: true, modules: {}}
? ? ? ? ? ? tmpModules = modules[splits[index]]
? ? ? ? }
? ? ? ? dynamicImportModules(tmpModules, file, splits, ++index)
? ? }
}
files.keys().filter(file => file != './index.js').forEach(file => {
? ? let splits = file.replace(/(\.\/|\.js)/g, '').split('\/');
? ? dynamicImportModules(dynamicModules, file, splits)
})
export default new Vuex.Store({
? ? modules: dynamicModules,
? ? strict: process.env.NODE_ENV !== 'production'
})

3. app.js文件內(nèi)容

const state = {
? ? user: {}, // 需要管理的狀態(tài)數(shù)據(jù)
}
const mutations = {
? ? setUser (state, val) {
? ? ? ? ? ? state.user = val
? ? ? ? }
}
const getters = {}
const actions = {}
export default {
? ? namespaced: true,
? ? state,
? ? mutations,
? ? getters,
? ? actions
}

4.1 使用 a.vue頁(yè)面

// 使用模塊中的mutations、getters、actions時(shí)候,要加上模塊名,例如使用commint執(zhí)行mutations時(shí)
// 格式: 模塊名/模塊中的mutations
this.$store.commit("app/setUser", user)
// 獲取屬性時(shí)同樣加上模塊名
this.$store.state.app.user?

4.2 utils.js中使用

// 引入 這里的store 就相當(dāng)于頁(yè)面中的 this.$store
import store from '@/store'
export const setCurUser = (user) => {
? ? let curUser = store.app.user
? ? if(!curUser) {
? ? ? ? store.commit("app/setUser", user)
? ? ? ? return user
? ? }
? ??
? ? return curUser
}

5. 配置main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
new Vue({
? el: '#app',
? router,
? store,
? render: h => h(App)
})

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • 詳解Vue2.0之去掉組件click事件的native修飾

    詳解Vue2.0之去掉組件click事件的native修飾

    這篇文章主要介紹了詳解Vue2.0之去掉組件click事件的native修飾,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 淺談vue 單文件探索

    淺談vue 單文件探索

    這篇文章主要介紹了淺談vue 單文件探索,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 解決vue-cli + webpack 新建項(xiàng)目出錯(cuò)的問(wèn)題

    解決vue-cli + webpack 新建項(xiàng)目出錯(cuò)的問(wèn)題

    下面小編就為大家分享一篇解決vue-cli + webpack 新建項(xiàng)目出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Vue之封裝公用變量以及實(shí)現(xiàn)方式

    Vue之封裝公用變量以及實(shí)現(xiàn)方式

    這篇文章主要介紹了Vue之封裝公用變量以及實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue對(duì)象的深層劫持詳細(xì)講解

    Vue對(duì)象的深層劫持詳細(xì)講解

    這篇文章主要介紹了vue2.x對(duì)象深層劫持的原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • vue查詢數(shù)據(jù)el-table不更新數(shù)據(jù)的解決方案

    vue查詢數(shù)據(jù)el-table不更新數(shù)據(jù)的解決方案

    這篇文章主要介紹了vue查詢數(shù)據(jù)el-table不更新數(shù)據(jù)的問(wèn)題及解決方案,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • Vue實(shí)現(xiàn)簡(jiǎn)單登錄界面

    Vue實(shí)現(xiàn)簡(jiǎn)單登錄界面

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單登錄界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • vue中使用loadsh的debounce防抖函數(shù)問(wèn)題

    vue中使用loadsh的debounce防抖函數(shù)問(wèn)題

    這篇文章主要介紹了vue中使用loadsh的debounce防抖函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 如何用Vue實(shí)現(xiàn)父子組件通信

    如何用Vue實(shí)現(xiàn)父子組件通信

    這篇文章主要介紹了如何用Vue實(shí)現(xiàn)父子組件通信,對(duì)Vue感興趣的同學(xué),可以參考下
    2021-05-05
  • vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求的方法

    vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求的方法

    這篇文章主要介紹了vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10

最新評(píng)論