Store state should always be a method now

This commit is contained in:
Sébastien Chopin 2017-05-21 15:45:21 +02:00
parent 8350dbfdbc
commit c3355e1467
8 changed files with 22 additions and 29 deletions

View File

@ -1,5 +0,0 @@
module.exports = {
generate: {
minify: false
}
}

View File

@ -1,10 +1,10 @@
export const state = { export const state = () => ({
list: [ list: [
'Lorem ipsum', 'Lorem ipsum',
'dolor sit amet', 'dolor sit amet',
'consetetur sadipscing elitr' 'consetetur sadipscing elitr'
] ]
} })
export const mutations = { export const mutations = {
add (state, title) { add (state, title) {

View File

@ -1,10 +1,10 @@
export const state = { export const state = () => ({
list: [ list: [
'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.', 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
'Lorem ipsum dolor sit amet, consetetur sadipscing elit.', 'Lorem ipsum dolor sit amet, consetetur sadipscing elit.',
'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
] ]
} })
export const mutations = { export const mutations = {
add (state, title) { add (state, title) {

View File

@ -1,4 +1,6 @@
export const state = { counter: 0 } export const state = () => ({
counter: 0
})
export const mutations = { export const mutations = {
increment (state) { increment (state) {

View File

@ -1,6 +1,6 @@
export const state = { export const state = () => ({
list: [] list: []
} })
export const mutations = { export const mutations = {
add (state, { text }) { add (state, { text }) {

View File

@ -13,7 +13,7 @@ let storeData = {}
// Check if store/index.js exists // Check if store/index.js exists
const indexFilename = filenames.find((filename) => filename.includes('./index.')) const indexFilename = filenames.find((filename) => filename.includes('./index.'))
if (indexFilename) { if (indexFilename) {
storeData = getModule(indexFilename, true) storeData = getModule(indexFilename)
} }
// If store is not an exported method = modules store // If store is not an exported method = modules store
@ -41,19 +41,19 @@ if (typeof storeData !== 'function') {
// createStore // createStore
export const createStore = storeData instanceof Function ? storeData : () => { export const createStore = storeData instanceof Function ? storeData : () => {
return new Vuex.Store(Object.assign({}, storeData, { return new Vuex.Store(Object.assign({}, storeData, {
// state: storeData.state instanceof Function ? storeData.state() : {} state: storeData.state instanceof Function ? storeData.state() : {}
})) }))
} }
// Dynamically require module // Dynamically require module
function getModule (filename, root) { function getModule (filename) {
const file = files(filename) const file = files(filename)
const module = file.default || file const module = file.default || file
// if (root && module.state && typeof module.state !== 'function') {
// throw new Error('[nuxt] root store state should be a function.')
// }
if (module.commit) { if (module.commit) {
throw new Error('[nuxt] store should export raw store options instead of an instance.') throw new Error('[nuxt] store/' + filename.replace('./', '') + ' should export a method which returns a Vuex instance.')
}
if (module.state && typeof module.state !== 'function') {
throw new Error('[nuxt] state should be a function in store/' + filename.replace('./', ''))
} }
return module return module
} }

View File

@ -1,8 +1,6 @@
export const state = () => { export const state = () => ({
return {
baz: 'Vuex Nested Modules' baz: 'Vuex Nested Modules'
} })
}
export const getters = { export const getters = {
baz (state) { baz (state) {

View File

@ -3,11 +3,9 @@ import Vuex from 'vuex'
Vue.use(Vuex) Vue.use(Vuex)
export const state = () => { export const state = () => ({
return {
counter: 1 counter: 1
} })
}
export const mutations = { export const mutations = {
increment (state) { increment (state) {