vue-cli3 public folder img doesn't show up

后端 未结 2 1257
陌清茗
陌清茗 2020-12-22 10:45

Does anyone know how to use the src attribute with an img file in a vue project\'s public folder? The browser is not finding the image.

I\'m using Vue CLI 3.7.0

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 11:30

    Update

    From the comments below, it seems you're running

    vue serve
    

    Only a Vue CLI project knows about the public directory and process.env.BASE_URL. You need to run your project instead of using the instant prototyping of vue serve.

    In other words, run

    npm run serve
    

    See https://cli.vuejs.org/guide/cli-service.html#using-the-binary


    If you have a file structure like this

    ├── public
    │   ├── favicon.html
    │   ├── index.html
    │   ├── logo.png
    

    then your code to display logo.png should look like

    
    
    
    

    See https://cli.vuejs.org/guide/html-and-static-assets.html#the-public-folder


    If you have your image in

    ├── public
    │   ├── img
    │   │   ├── logo.png
    

    then your component template code becomes

    
    

提交回复
热议问题