How can I get vue-loader to interpolate asset urls?

后端 未结 2 1001
花落未央
花落未央 2021-01-14 07:24

I have an image with a dynamic (interpolated) src attribute.

How do I get vue-loader to interpo

2条回答
  •  死守一世寂寞
    2021-01-14 07:45

    I'm pretty sure that your code throws a warning (not referred it):

    [Vue warn]: src="./{{bar}}.jpg": interpolation in "src" attribute will cause a 404 request. Use v-bind:src instead.

    So, you should bind the value:

    
    

    The above example it loads an image xxx.jpg from the static directory assets, but not via loader yet.

    To accomplish that, you should use a dynamic require:

    
    
    methods: {
      loadImage (imgName) {
        return require('./assets/' + imgName + '.jpg')
      }
    }
    

    NOTE

    It is not recommended if the directory contains a large number of files, because the webpack will be load all the files which match your request (for the above example: ./assets/*.jpg).

提交回复
热议问题