简体   繁体   English

使用vue.js将`id`绑定到img src url

[英]Bind `id` to img src url using vue.js

I'm new to vue, and trying to follow a tutorial. 我是vue的新手,并试图遵循教程。 I'm trying to pull the image through that relates to the id of my article. 我试图通过与我文章的内容相关的图像。

Everything else is coming through ok, article.id etc. The image will have the same filename as the article.id for the time being, so I just want to pull it into the component. 其他所有东西都通过ok, article.id等。图像暂时具有与article.id相同的文件名,所以我只想把它拉进组件中。

<div class="card bg-dark mb-2" v-for="article in articles" v-bind:key="article.id">
  <div class="card-body">
    <img class="card-img-top" v-bind:src="images/articles/`{{ $article.id }}`.jpg" width="100%" alt="Card image cap" />
    <h5 class="card-title  text-white">{{ article.id }}. {{ article.title }}</h5>
    <h6 class="card-subtitle mb-2 text-white-50">{{ article.from }}</h6>
    <p class="card-text text-truncate" style="max-width: 150px;">{{ article.description }}</p>
  </div>
  <div class="card-footer text-right">
    <small class="text-white-50">Added by <span class="text-white">{{ article.added_by }}</span> ({{ article.created_at }})</small>
  </div>
</div>

Please let me know if I need to provide anything else to this post. 如果我需要在这篇文章中提供任何其他内容,请告诉我。

Update: 更新:

I don't know if it makes a difference, but the article.id I'm using is being fetched from the Laravel API I've created. 我不知道它是否有所作为,但我正在使用的article.id是从我创建的Laravel API获取的。

The #app div is in in my index.blade.php file #app div位于我的index.blade.php文件中

export default {
data() {
  return {
    articles: [],
    article: {
      id: '',
      title: '',
      description: '',
      from: '',
      rating: '',
      from: '',
      created_at: '',
      added_by: ''
    },
    recipe_id: '',
    pagination: {},
    edit: false
  }
},

created() {
  this.fetchArticles();
}, 

methods: {
  fetchArticles(page_url) {
    let vm = this;
    page_url = page_url || '/api/articles'
    fetch(page_url)
    .then(res => res.json())
    .then(res => {
      this.articles = res.data;
      vm.makePagination(res.meta, res.links);
    })
    .catch(err => console.log(err))
  },

  makePagination(meta, links) {
    let pagination = {
      current_page : meta.current_page,
      last_page : meta.last_page,
      next_page_url : links.next,
      prev_page_url : links.prev
    };

    this.pagination = pagination;
  }
}

A bound attribute is parsed as javascript. 绑定属性被解析为javascript。 So if you want the string images/articles/123.jpg for article 123 you need to pass it to the :src attribute like so: 因此,如果你想要文章123的字符串images/articles/123.jpg ,你需要将它传递给:src属性,如下所示:

<img :src="`images/articles/${article.id}.jpg`" />

or 要么

<img :src="'images/articles/' + article.id + '.jpg'" />

Mustaches cannot be used inside HTML attributes. Mustaches不能在HTML属性中使用。 Instead, use a v-bind directive (v-bind:src or :src) 相反,使用v-bind指令(v-bind:src或:src)

You can bind the image src like this: 您可以像这样绑定图像src:

<img :src="'images/articles/' + article.id  + '.jpg'" />

You can see this working on the screenshot of the fiddle I developed for your answer 你可以看到这个工作在我为你的答案开发的小提琴的截图

Check out the Fiddle :) 看看小提琴:)

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

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