Store directory

The store directory contains your Vuex Store files. The Vuex Store comes with Nuxt out of the box but is disabled by default. Creating an index.js file in this directory enables the store.


Store directory - 图1

This directory cannot be renamed without extra configuration.

Using a store to manage the state is important for every big application. That’s why Nuxt implements Vuex in its core.

Activate the Store

Nuxt will look for the store directory. If it contains a file, that isn’t a hidden file or a README.md file, then the store will be activated. This means that Nuxt will:

  1. Import Vuex,
  2. Add the store option to the root Vue instance.

Modules

Every .js file inside the store directory is transformed as a namespaced module (index being the root module). Your state value should always be a function to avoid unwanted shared state on the server side.

To get started, export the state as a function, and the mutations and actions as objects.

store/index.js

  1. export const state = () => ({
  2. counter: 0
  3. })
  4. export const mutations = {
  5. increment(state) {
  6. state.counter++
  7. }
  8. }

Then, you can have a store/todos.js file:

store/todos.js

  1. export const state = () => ({
  2. list: []
  3. })
  4. export const mutations = {
  5. add(state, text) {
  6. state.list.push({
  7. text,
  8. done: false
  9. })
  10. },
  11. remove(state, { todo }) {
  12. state.list.splice(state.list.indexOf(todo), 1)
  13. },
  14. toggle(state, todo) {
  15. todo.done = !todo.done
  16. }
  17. }

The store will be created as such:

  1. new Vuex.Store({
  2. state: () => ({
  3. counter: 0
  4. }),
  5. mutations: {
  6. increment(state) {
  7. state.counter++
  8. }
  9. },
  10. modules: {
  11. todos: {
  12. namespaced: true,
  13. state: () => ({
  14. list: []
  15. }),
  16. mutations: {
  17. add(state, { text }) {
  18. state.list.push({
  19. text,
  20. done: false
  21. })
  22. },
  23. remove(state, { todo }) {
  24. state.list.splice(state.list.indexOf(todo), 1)
  25. },
  26. toggle(state, { todo }) {
  27. todo.done = !todo.done
  28. }
  29. }
  30. }
  31. }
  32. })

And in your pages/todos.vue, using the todos module:

pages/todos.vue

  1. <template>
  2. <ul>
  3. <li v-for="todo in todos" :key="todo.text">
  4. <input :checked="todo.done" @change="toggle(todo)" type="checkbox">
  5. <span :class="{ done: todo.done }">{{ todo.text }}</span>
  6. </li>
  7. <li><input @keyup.enter="addTodo" placeholder="What needs to be done?"></li>
  8. </ul>
  9. </template>
  10. <script>
  11. import { mapMutations } from 'vuex'
  12. export default {
  13. computed: {
  14. todos () {
  15. return this.$store.state.todos.list
  16. }
  17. },
  18. methods: {
  19. addTodo (e) {
  20. this.$store.commit('todos/add', e.target.value)
  21. e.target.value = ''
  22. },
  23. ...mapMutations({
  24. toggle: 'todos/toggle'
  25. })
  26. }
  27. }
  28. </script>
  29. <style>
  30. .done {
  31. text-decoration: line-through;
  32. }
  33. </style>

The module method also works for top-level definitions without implementing a sub-directory in the store directory.

Example for state: you create a file store/state.js and add the following.

  1. export default () => ({
  2. counter: 0
  3. })

And the corresponding mutations can be in the file store/mutations.js

store/mutations.js

  1. export default {
  2. increment(state) {
  3. state.counter++
  4. }
  5. }

Example folder structure

A complex store setup file/folder structure might look like this:

  1. store/
  2. --| index.js
  3. --| ui.js
  4. --| shop/
  5. ----| cart/
  6. ------| actions.js
  7. ------| getters.js
  8. ------| mutations.js
  9. ------| state.js
  10. ----| products/
  11. ------| mutations.js
  12. ------| state.js
  13. ------| itemsGroup1/
  14. --------| state.js

Plugins in the Store

You can add additional plugins to the store by putting them into the store/index.js file:

store/index.js

  1. import myPlugin from 'myPlugin'
  2. export const plugins = [myPlugin]
  3. export const state = () => ({
  4. counter: 0
  5. })
  6. export const mutations = {
  7. increment(state) {
  8. state.counter++
  9. }
  10. }

More information about the plugins: Vuex documentation.

The nuxtServerInit Action

If the action nuxtServerInit is defined in the store and the mode is universal, Nuxt will call it with the context (only from the server-side). It’s useful when we have some data on the server we want to give directly to the client-side.

For example, let’s say we have sessions on the server-side and we can access the connected user through req.session.user. To add the authenticated user to our store, we update our store/index.js to the following:

store/index.js

  1. actions: {
  2. nuxtServerInit ({ commit }, { req }) {
  3. if (req.session.user) {
  4. commit('user', req.session.user)
  5. }
  6. }
  7. }

Store directory - 图2

Only the primary module (in store/index.js) will receive this action. You’ll need to chain your module actions from there.

The context is given to nuxtServerInit as the 2nd argument in the asyncData method.

If nuxt generate is ran, nuxtServerInit will be executed for every dynamic route generated.

Store directory - 图3

Asynchronous nuxtServerInit actions must return a Promise or leverage async/await to allow the nuxt server to wait on them.

store/index.js

  1. actions: {
  2. async nuxtServerInit({ dispatch }) {
  3. await dispatch('core/load')
  4. }
  5. }

Vuex Strict Mode

Strict mode is enabled by default on dev mode and turned off in production mode. To disable strict mode in dev, follow the below example in store/index.js:

  1. export const strict = false