Search code examples
javascriptvue.jsvue-clivue-cli-3

Vue-cli: how to use pictures' paths from local json as src


I have a json file with this structure:

[
  {
    "id": 1,
    "name": "name1",
    "address": "require(./assets/pic1.jpg)"
  }
]

I import it:

import data from "./assets/test.json"

And then try to use as src for my image:

<img :src='item.address'>

But image doesn't appear. Could anybody say how to solve this problem?


Solution

  • You can solve this by changing your json file and image element:

    [
      {
        "id": 1,
        "name": "name1",
        "address": 'pic1.jpg'
      }
    ]
    
    <img :src="require(`./assets/${item.address}`)" >
    

    See more here: https://github.com/vuejs/Discussion/issues/202