examples: improve vuex store example (#5017)

This commit is contained in:
Alexander Lichter 2019-02-19 09:35:51 +00:00 committed by Xin Du (Clark)
parent b1753e4eae
commit a9511e58b2
15 changed files with 39 additions and 115 deletions

View File

@ -1,7 +0,0 @@
# Nuxt with [Vuex](https://vuex.vuejs.org/) and store modules
> Vuex is a state management pattern + library for Vue.js applications.
Read on Vuex modules [here](https://vuex.vuejs.org/guide/modules.html)
https://nuxtjs.org/guide/vuex-store#modules-files

View File

@ -1,12 +0,0 @@
{
"name": "example-vuex-store-modules",
"dependencies": {
"nuxt": "latest"
},
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"post-update": "yarn upgrade --latest"
}
}

View File

@ -1,12 +0,0 @@
<template>
<div>
<p>
<button @click="$store.commit('increment')">
{{ $store.state.counter }}
</button><br>
<NuxtLink to="/">
Home
</NuxtLink>
</p>
</div>
</template>

View File

@ -1,45 +0,0 @@
<template>
<div>
<p /><h3>Index Module</h3>
<button @click="increment">
{{ counter }}
</button>
<br>
<NuxtLink to="/about">
About
</NuxtLink>
<br>
<br>
<h3>Todo Module</h3>
<NuxtLink to="/todos">
Todos
</NuxtLink>
<br>
<br>
<h3>Nested Modules</h3>
<NuxtLink to="/website">
Website
</NuxtLink>
</p>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: mapState([
'counter'
]),
// fetch(context) is called by the server-side
// and before instantiating the component
fetch({ store }) {
store.commit('increment')
},
methods: {
increment() {
this.$store.commit('increment')
}
}
}
</script>

View File

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

View File

@ -1,7 +1,7 @@
# Nuxt with [Vuex](https://vuex.vuejs.org/)
# Nuxt with [Vuex](https://vuex.vuejs.org/) and store modules
> Vuex is a state management pattern + library for Vue.js applications.
>Vuex is a state management pattern + library for Vue.js applications.
Read on Vuex modules [here](https://vuex.vuejs.org/guide/modules.html)
https://nuxtjs.org/examples/vuex-store
https://nuxtjs.org/guide/vuex-store#modules-files

View File

@ -1,13 +1,25 @@
<template>
<div>
<p>
<button @click="increment">
{{ counter }}
</button><br>
<NuxtLink to="/about">
About
</NuxtLink>
</p>
<h3>Index Module</h3>
<button @click="increment">
{{ counter }}
</button>
<br>
<NuxtLink to="/about">
About
</NuxtLink>
<br>
<br>
<h3>Todo Module</h3>
<NuxtLink to="/todos">
Todos
</NuxtLink>
<br>
<br>
<h3>Nested Modules</h3>
<NuxtLink to="/website">
Website
</NuxtLink>
</div>
</template>
@ -19,7 +31,7 @@ export default {
'counter'
]),
// fetch(context) is called by the server-side
// and nuxt before instantiating the component
// and before instantiating the component
fetch({ store }) {
store.commit('increment')
},

View File

@ -6,12 +6,6 @@ export const state = () => ({
]
})
export const mutations = {
add(state, title) {
state.list.push(title)
}
}
export const getters = {
get(state) {
return state.list

View File

@ -0,0 +1,5 @@
export default {
add(state, title) {
state.list.push(title)
}
}

View File

@ -0,0 +1,5 @@
export default {
todos(state) {
return state.list
}
}

View File

@ -1,8 +1,4 @@
export const state = () => ({
list: []
})
export const mutations = {
export default {
add(state, { text }) {
state.list.push({
text,
@ -14,9 +10,3 @@ export const mutations = {
todo.done = !todo.done
}
}
export const getters = {
todos(state) {
return state.list
}
}

View File

@ -0,0 +1,3 @@
export default () => ({
list: []
})