最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

vuex使用modules時遇到的坑

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

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

vuex中modules基本用法

1. store文件結構

- 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文件內容

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 就相當于頁面中的 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • Vue 理解之白話 getter/setter詳解

    Vue 理解之白話 getter/setter詳解

    這篇文章主要介紹了Vue getter setter,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue不用window的方式如何刷新當前頁面

    vue不用window的方式如何刷新當前頁面

    這篇文章主要介紹了vue不用window的方式如何刷新當前頁面,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue是怎么渲染template內的標簽內容的

    Vue是怎么渲染template內的標簽內容的

    這篇文章主要介紹了Vue是怎么渲染template內的標簽內容的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • 詳解vuejs幾種不同組件(頁面)間傳值的方式

    詳解vuejs幾種不同組件(頁面)間傳值的方式

    本篇文章主要介紹了詳解vuejs幾種不同組件(頁面)間傳值的方式,具有一定的參考價值,有興趣的可以了解一下
    2017-06-06
  • vue如何通過params和query傳值(刷新不丟失)

    vue如何通過params和query傳值(刷新不丟失)

    這篇文章主要介紹了vue如何通過params和query傳值(刷新不丟失),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3自己封裝面包屑功能組件的幾種方式

    vue3自己封裝面包屑功能組件的幾種方式

    網(wǎng)站中我們經(jīng)常看到有個導航路徑,可以直觀地顯示當前頁面的路徑,并快速返回之前的任意頁面,這是一個非常實用的功能,也是在Web前端必備的導航UI之一,這篇文章主要給大家介紹了關于vue3自己封裝面包屑功能組件的幾種方式,需要的朋友可以參考下
    2021-09-09
  • Vue新一代狀態(tài)管理工具Pinia的具體使用

    Vue新一代狀態(tài)管理工具Pinia的具體使用

    本文主要介紹了Vue新一代狀態(tài)管理工具Pinia的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • vue3+ts+EsLint+Prettier規(guī)范代碼的方法實現(xiàn)

    vue3+ts+EsLint+Prettier規(guī)范代碼的方法實現(xiàn)

    本文主要介紹在Vue3中使用TypeScript做開發(fā)時,如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。感興趣的可以了解一下
    2021-10-10
  • vite+vue3+element-plus項目搭建的方法步驟

    vite+vue3+element-plus項目搭建的方法步驟

    因為vue3出了一段時間了,element也出了基于vue3.x版本的element-plus,vite打包聽說很快,嘗試一下,感興趣的可以了解一下
    2021-06-06
  • 詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果

    詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果

    這篇文章主要介紹了詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論

讷河市| 新余市| 获嘉县| 永顺县| 宁国市| 沂源县| 南充市| 灵川县| 姚安县| 镇巴县| 花莲县| 河西区| 海南省| 蒲江县| 个旧市| 南岸区| 海南省| 安西县| 深圳市| 时尚| 临泽县| 贡觉县| 大余县| 德昌县| 维西| 临沭县| 绿春县| 湄潭县| 深水埗区| 六盘水市| 张北县| 普陀区| 青浦区| 高陵县| 宕昌县| 旬邑县| 崇明县| 新民市| 称多县| 定西市| 五莲县|