代码之家  ›  专栏  ›  技术社区  ›  Collin Allen

如何使用Nuxt访问asyncData中的数据

  •  0
  • Collin Allen  · 技术社区  · 7 年前

    data ,并在我的 asyncData

    <script>
    export default {
      async asyncData ({ $axios, params }) {
        const things = await $axios.$get(`/api/things`, {
          params: {
            sort_column: this.sortColumn,
            sort_ascending: this.sortAscending,
          }
        });
        return { things };
      },
      data () {
        return {
          sortColumn: 'created_at',
          sortAscending: true
        }
      },
      // ...
    }
    </script>
    

    但看来 尚未提供,因为 this.sortColumn this.sortAscending 未定义。我如何访问这些默认值时 异步数据

    注意:有人问这个问题 here ,但公认的答案与这种情况无关。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Aldarund    7 年前

    async asyncData ({ $axios, params }) {
        const sortColumn = 'created_at'
        const sortAscending = true
        const things = await $axios.$get(`/api/things`, {
          params: {
            sort_column: sortColumn,
            sort_ascending: this.sortAscending,
          }
        });
        return { things, sortColumn, sortAscending };
      },
    

    推荐文章