简体   繁体   English

Router-link 动态 vueJS

[英]Router-link dynamic vueJS

I'm new to vueJS.我是 vueJS 的新手。 I am creating a news system to train myself.我正在创建一个新闻系统来训练自己。 I'm a little problem.我有点问题。 Here is the link that allows me to go to the detail of an article:这是允许我转到文章详细信息的链接:

<router-link :to="{ name: 'Blog Details', params: { id: 1 }}"><img v-bind:src="postThumbnail" v-bind:alt="title"></router-link>

My component on which this link is located, has several props including the article id (actu_id).我的这个链接所在的组件有几个道具,包括文章 ID (actu_id)。

In the link to the article, I would like the id located in the params not to be hard "1", but actu_id.在文章的链接中,我希望参数中的 id 不是硬“1”,而是 actu_id。

I do not know how to do.我不知道该怎么办。

When building things like this, first think how the data gets to your page.在构建这样的东西时,首先要考虑数据如何到达您的页面。 It's probably most efficient to use loops here, assuming you'll have multiple blog posts.假设您有多个博客文章,在这里使用循环可能是最有效的。 Save them in the data in an array:将它们保存在数组中的数据中:

    blogs: [
      {
        name: "Post 1",
        id: 1,
        thumbnail: "agsrgsghr.jpg",
      },
      {
        name: "Post 2",
        id: 2,
        thumbnail: "agsrgsghr2.jpg",
      },
    ],

And you can use template literals to set the router link parameters您可以使用模板文字来设置路由器链接参数

  <div v-for="blog in blogs" :key="blog.id">
      <router-link :to="{ name: `${blog.name}`, params: { id: `${blog.id}` } }"
        ><img :src="blog.thumbnail" :alt="blog.name"
      /></router-link>
    </div>

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

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