简体   繁体   English

如何显示来自 nuxt js 商店的数据?

[英]How to show data from nuxt js store?

I new on Nuxt JS and try tutorial on nuxt website, i make store in store/index.js我是 Nuxt JS 的新手,并在 nuxt 网站上尝试教程,我在 store/index.js 中进行存储

export const state = () => ({
  mountain: [],
})

export const mutations = {
  addMountain(state, mountain) {
    state.mountain.push(mountain)
  },
}

export const actions = {
  async fetchMountain() {
    const mountain = await fetch("https://api.nuxtjs.dev/mountains").then(
      (res) => res.json()
    );
    // this.mountain = mountain
  }
}

after that i make page on pages/index.js之后我在 pages/index.js 上创建页面

<template>
  <div>
    <h1>Nuxt Mountains</h1>
    <ul>
      <li v-for="mount of mountain">{{ mount.title }}</li>
    </ul>
    <button @click="$fetch">Refresh</button>
  </div>
</template>

<script>
import $store from "~/store";

export default {
  fetch() {
    return $store.state.mountain;
  },
};
</script>

but i dont see anyting?但我什么也没看到? someone can help me有人可以帮助我

在此处输入图像描述

This is how you can achieve this example.这就是实现此示例的方法。

/pages/index.vue

<template>
  <div>
    <h1>Nuxt Mountains</h1>
    <p v-show="$fetchState.pending">Loading mountains...</p>

    <ul v-show="!$fetchState.pending">
      <li v-for="mountain of mountains" :key="mountain.slug">
        {{ mountain.title }}
      </li>
    </ul>

    <hr />
    <button @click="$fetch">Refresh</button>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  async fetch() {
    await this.fetchMountains()
  },
  computed: {
    ...mapState(['mountains']),
  },
  methods: {
    ...mapActions(['fetchMountains']),
  },
}
</script>

Few notes on above:以上几点注意事项:

  • mapState and mapActions is not mandatory here, you could access them via $store.dispatch etc directly but it's IMO more clean and quite explicit/good practice mapStatemapActions在这里不是强制性的,您可以通过$store.dispatch等直接访问它们,但它是 IMO 更干净且非常明确/良好的做法
  • don't forget the :key on the v-for , it's mandatory不要忘记v-for上的:key ,这是强制性的
  • prefer to use async/await everywhere rather than a mix with .then更喜欢在任何地方使用async/await而不是与.then混合使用

/store/index.js

export const state = () => ({
  mountains: [],
})

export const mutations = {
  SET_MOUNTAINS(state, mountains) {
    state.mountains = mountains
  },
}

export const actions = {
  async fetchMountains({ commit }) {
    const response = await fetch('https://api.nuxtjs.dev/mountains')
    const mountains = await response.json()
    commit('SET_MOUNTAINS', mountains)
  },
}

Few notes on above:以上几点注意事项:

  • using UPPER_SNAKE_CASE is a nice convention for mutations使用UPPER_SNAKE_CASE是一个很好的突变约定
  • again, async + await combo再次, async + await组合
  • mountains (plural) seems more appropriate because we will have several ones mountains (复数)似乎更合适,因为我们会有几个
  • calling the mutation after the HTTP call in the action is the usual way to go with Nuxt2在操作中调用 HTTP 之后调用突变是使用 Nuxt2 调用 go 的常用方法

the store object already exists within your context.商店 object 已经存在于您的上下文中。 so you don't need to import it at all... example of how to use it:所以你根本不需要导入它......如何使用它的例子:

 computed: {
     mountain () {
           return this.$store.state.mountain;
     }
  }

ps.附言。 why inside the computed?为什么在计算里面? because we don't need the component to be rerendered every time an update happens to the module.因为我们不需要在每次模块发生更新时重新渲染组件。 only if the data you are accessing got updated that should trigger rerendering...仅当您正在访问的数据得到更新时才会触发重新渲染...

but that is also not the best practice.但这也不是最佳做法。 you should always use modules.你应该总是使用模块。


to understand that better you should know that nuxt imports all the files inside the store folder automatically.为了更好地理解这一点,您应该知道 nuxt 会自动导入store文件夹中的所有文件。

so every file you create inside the store file will be used as a new module in vuex store.因此,您在 store 文件中创建的每个文件都将用作 vuex store 中的新模块。

so for instance if you created a file called todo.js inside the store folder you will access it anywhere in the project using the following:因此,例如,如果您在store文件夹中创建了一个名为todo.js的文件,您将使用以下命令在项目的任何位置访问它:

 computed: {
     mountain () {
           return this.$store.state.todo.mountain;
     }
  }

i would suggest you take a look into that from here: https://nuxtjs.org/docs/directory-structure/store/我建议您从这里查看一下: https://nuxtjs.org/docs/directory-structure/store/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM