Can´t load the "src" of an image via json (Vue.js)

javascript html json vue.js

1424 观看


12 作者的声誉

var topArticle=new Vue({
  created: function(){
              .then(res=>{this.topmostArticle=$.grep(res,function(e){return e.topnews!==" "});
<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>The greatest news app ever</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>

    <div id  ="toparticle">
      <img src="{{topmostArticle[0].topnews[0].img}}">
      <img src="2.jpg">

    <script src="main.js"></script>


I'm trying to load the source for an image with an external file but I'm facing some problems...the source isn't recognized in the image tag. The code should be correct because when the data is loaded outside the image tag it works nice. Anyone has any idea?? Thank you so much!

The first image is not loaded (because the src is transmitted via json), the second is ok and the third command, "{{topmostArticle[0].topnews[0].img}}", returns "2.jpg" so it's ok



The question is why I can't load the src directly through json? Please click on the links below(web view) for more clarity!

  [web view][1]

作者: j.joe 的来源 发布者: 2017 年 12 月 27 日

回应 (2)


302 作者的声誉

Try changing the way that you bind the attribute:

<img :src="topmostArticle[0].topnews[0].img">
作者: Haroon R. 发布者: 27.12.2017 06:03


170 作者的声誉

Easiest way to get the image

<img :src="require(`@/assets/${topnews.img}`)" alt="Top News" />
作者: Kapil Chhabra 发布者: 19.02.2019 11:03