mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-06 21:10:38 +00:00
[Store] Fix createStore bug
This commit is contained in:
parent
3b175ee80f
commit
2cdeab5661
@ -1,15 +1,56 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
|
|
||||||
Vue.use(Vuex)
|
Vue.use(Vuex)
|
||||||
|
|
||||||
let files = require.context('~/store', true, /^\.\/.*\.(js|ts)$/)
|
// Recursive find files in ~/store
|
||||||
let filenames = files.keys()
|
const files = require.context('~/store', true, /^\.\/.*\.(js|ts)$/)
|
||||||
|
const filenames = files.keys()
|
||||||
|
|
||||||
|
// Store
|
||||||
|
let storeData = {}
|
||||||
|
|
||||||
|
// Check if store/index.js exists
|
||||||
|
if (filenames.indexOf('./index.js') !== -1) {
|
||||||
|
storeData = getModule('./index.js')
|
||||||
|
storeData.state = storeData.state() // Vuex Bug!
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store modules
|
||||||
|
if (!storeData.modules) {
|
||||||
|
storeData.modules = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let filename of filenames) {
|
||||||
|
let name = filename.replace(/^\.\//, '').replace(/\.(js|ts)$/, '')
|
||||||
|
if (name === 'index') continue
|
||||||
|
|
||||||
|
let namePath = name.split(/\//)
|
||||||
|
let module = getModuleNamespace(storeData, namePath)
|
||||||
|
|
||||||
|
name = namePath.pop()
|
||||||
|
module[name] = getModule(filename)
|
||||||
|
module[name].namespaced = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// createStore
|
||||||
|
export const createStore = storeData === 'function' ? storeData : () => new Vuex.Store(storeData)
|
||||||
|
|
||||||
|
// Dynamically require module
|
||||||
function getModule (filename) {
|
function getModule (filename) {
|
||||||
let file = files(filename)
|
const file = files(filename)
|
||||||
return file.default
|
const module = Object.assign({}, file.default || file)
|
||||||
? file.default
|
if (module.state && typeof module.state !== 'function') {
|
||||||
: file
|
// eslint-disable-next-line no-console
|
||||||
|
console.error('[nuxt] store state should be a function.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (module.commit) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error('[nuxt] store should export raw store options instead of an instance.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return module
|
||||||
}
|
}
|
||||||
|
|
||||||
function getModuleNamespace (storeData, namePath) {
|
function getModuleNamespace (storeData, namePath) {
|
||||||
@ -22,43 +63,3 @@ function getModuleNamespace (storeData, namePath) {
|
|||||||
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
|
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
|
||||||
return getModuleNamespace(storeData.modules[namespace], namePath)
|
return getModuleNamespace(storeData.modules[namespace], namePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
let store
|
|
||||||
let storeData = {}
|
|
||||||
|
|
||||||
// Check if store/index.js returns a vuex store
|
|
||||||
if (filenames.indexOf('./index.js') !== -1) {
|
|
||||||
let mainModule = getModule('./index.js')
|
|
||||||
if (mainModule.commit) {
|
|
||||||
console.error('[nuxt.js] store/index should export raw store options instead of an instance.')
|
|
||||||
} else {
|
|
||||||
if (mainModule.state && typeof mainModule.state !== 'function') {
|
|
||||||
console.error('[nuxt.js] store state should be a function.')
|
|
||||||
}
|
|
||||||
storeData = mainModule
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the store if there is no store yet
|
|
||||||
if (store == null) {
|
|
||||||
storeData.modules = storeData.modules || {}
|
|
||||||
for (let filename of filenames) {
|
|
||||||
let name = filename.replace(/^\.\//, '').replace(/\.(js|ts)$/, '')
|
|
||||||
if (name === 'index') continue
|
|
||||||
|
|
||||||
let namePath = name.split(/\//)
|
|
||||||
let module = getModuleNamespace(storeData, namePath)
|
|
||||||
|
|
||||||
name = namePath.pop()
|
|
||||||
module[name] = getModule(filename)
|
|
||||||
module[name].namespaced = true
|
|
||||||
|
|
||||||
if (typeof module[name].state !== 'function') {
|
|
||||||
console.error('[nuxt.js] store module state should be a function.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createStore () {
|
|
||||||
return new Vuex.Store(storeData)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user