vue.js - Nuxt : How to create links in a loop using the data as the link reference

标签 vue.js nuxt.js

我正在从数据源(使用 $content)读取一组链接,并希望生成一个 HTML 元素列表,这些元素将链接到相应的页面。

// test.yaml in content directory
links:
  - id: a
    title: A
  - id: b
    title: B
  - id: p
    title: P

现在,我想遍历这些数据并生成一组 HTML 链接

<!-- page.vue in pages directory -->
 <template>
   <div v-for="link in this._links" :key="link.id">
      <NuxtLink to="/whatToPutHere">{{link.title}}</NuxtLink>
   </div>
 </template>
// script
 <script>
   export default {
      async asyncData({ $content, params }) {
         const _links = await $content("test").fetch();
         return _links;
      }
   };
 </script>

对于每个项目,我都想要一个链接,例如:

  • 对于id a,链接应该是/content/a
  • 对于id b,链接应该是/content/b

假设上述链接的 slug 存在并且页面按预期工作。谢谢

最佳答案

你可以使用

<NuxtLink :to="`/content/${link.id}`">{{link.title}}</NuxtLink>

它应该可以工作

在vue中,你不需要添加整个v-bind:tag,可以只使用:tag
此外,如果您 v-bind 任何标记,引号之间的任何内容都将是 javascript 代码。

关于vue.js - Nuxt : How to create links in a loop using the data as the link reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70810476/

相关文章:

javascript - 在 vue 中完成 v-for 后运行 jQuery 函数?

javascript - vue-test-utils: could not overwrite property $route, 这通常是由于插件将属性添加为只读值引起的

vue.js - Nuxt 的 Vuex 中的项目是否正在被抓取?

typescript - TS2749 : 'XXX' refers to a value, 但在这里被用作类型。您的意思是 'typeof XXX' 吗?

vue.js - Nuxt Vuex Store Cookie 问题

vue.js - 开玩笑单元测试 : wrapper. vm.$refs.editForm.validate 不是函数

javascript - 单击事件时从 jquery 触发 vue 方法

javascript - Vuetify,如何通过双击 v-data-table 中的事件获取行

vue.js - 为什么我的 `client-only` 组件在 nuxt 提示 `window is not defined` ?

node.js - Vuejs/Nuxt 中每个页面的元数据一直显示为 "## Build Setup"?