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

vuex中使用modules時遇到的坑及問題

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

vuex使用modules時遇到的坑

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

屬性,否則命名無法暴露出來,導(dǎo)致報[vuex] module namespace not found in mapState()等錯誤。

vuex中modules基本用法

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

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

2.1 index.js中-手動引入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)模塊注冊
? ? ? ? namespaced: true, // 為了解決不同模塊命名沖突的問題
? ? ? ? app,
? ? ? ? bus
? ? }
})

2.2 index.js中-動態(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頁面

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

4.2 utils.js中使用

// 引入 這里的store 就相當(dāng)于頁面中的 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)
})

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

相關(guān)文章

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

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

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

    淺談vue 單文件探索

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

    解決vue-cli + webpack 新建項目出錯的問題

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

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

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

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

    這篇文章主要介紹了vue2.x對象深層劫持的原理實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(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ù)的問題及解決方案,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Vue實現(xiàn)簡單登錄界面

    Vue實現(xiàn)簡單登錄界面

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

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

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

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

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

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

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

最新評論