[Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js相关的知识,希望对你有一定的参考价值。

You often use the same data in different ways across pages. This lesson walks you through setting up multiple pages, retrieving the same data, then displaying it for each page‘s use-case.

 

index.vue:

<template>
  <div>
    <form @submit.prevent="add(task)">
      <input v-model="task" type="text" />
      <input type="submit" value="ADD">
    </form>
    <article class="pa3 pa5-ns">
      <ul class="list pl0 ml0 center mw6 ba b--light-silver br2">
        <li v-for="todo of todos" class="flex items-center ph3 pv3 bb b--light-silver">
          <span v-bind:class="{strike: todo.complete}" class="flex-auto">{{todo.id}} {{todo.task}}</span>
          <button @click="toggle(todo)"><img src="https://icon.now.sh/check" alt="toggle"></button>
          <button @click="remove(todo)"><img src="https://icon.now.sh/trash" alt="delete"></button>
        </li>
      </ul>
    </article>
  </div>
</template>

<script>
  import { mapState, mapActions } from vuex
  import {init} from ./shared

  export default {
    fetch: init,
    computed: {
      ...mapState({
        todos: (state) => state.todos
      })
    },
    data () {
      return {
        task: Some data
      }
    },
    methods: {
      ...mapActions([
        add,
        remove,
        toggle
      ])
    }
  }
</script>

 

active.vue:

<template>
  <div>
    <article class="pa3 pa5-ns">
      <ul class="list pl0 ml0 center mw6 ba b--light-silver br2">
        <li v-for="todo of todos" class="flex items-center ph3 pv3 bb b--light-silver">
          <span class="flex-auto">{{todo.id}} {{todo.task}}</span>
        </li>
      </ul>
    </article>
  </div>
</template>

<script>
  import { mapState } from vuex
  import {init} from ./shared

  export default {
    fetch: init,
    computed: {
      ...mapState({
        todos: (state) => state.todos.filter(t => !t.complete)
      })
    }
  }
</script>

 

completed.vue:

<template>
  <div>
    <article class="pa3 pa5-ns">
      <ul class="list pl0 ml0 center mw6 ba b--light-silver br2">
        <li v-for="todo of todos" class="flex items-center ph3 pv3 bb b--light-silver">
          <span class="flex-auto">{{todo.id}} {{todo.task}}</span>
        </li>
      </ul>
    </article>
  </div>
</template>

<script>
  import { mapState } from vuex
  import {init} from ./shared

  export default {
    fetch: init,
    computed: {
      ...mapState({
        todos: (state) => state.todos.filter(t => t.complete)
      })
    }
  }
</script>

 

以上是关于[Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js的主要内容,如果未能解决你的问题,请参考以下文章

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

Nuxt 等待异步 + vuex

store/ 的 Vuex 经典模式已弃用,将在 Nuxt 3 中删除

错误:[vuex] 不要在突变处理程序之外改变 vuex 存储状态。 NUXT

Vue/Nuxt:如何在 vuex 商店中访问 Nuxt 实例?

Nuxt + Vuex - 如何将 Vuex 模块分解为单独的文件?