解决.vue文件url引用文件的问题

栏目: JavaScript · 发布时间: 6年前

内容简介:遇到的问题:根据开发环境截图:

解决.vue文件url引用文件的问题

遇到的问题:

  • 在css中引入图片,明明目录结构是对的,还是 This dependency was not found
  • dev 好好的, build 之后凉凉,图片加载 404

添加图片路径配置

webpack 添加 alias

//webpack.base.conf.js
    alias: {
      '@': resolve('src'),
      //加入
      'assets': resolve('src/assets')
    }

路径书写规则

  • template 可使用 @~
  • style 只能使用 ~
  • script 只能使用 @
  • 不需要经过打包的 static 文件写相对路径

根据 limit:10000 ,使用两张图片:

<template>
    <div>
        <div>
            img+src:@
            <img src="@/assets/images/jiaban.jpg" height="200px">
            <img src="@/assets/images/cat.png" alt="">
        </div>
        <div>
            img+src:~
            <img src="~assets/images/jiaban.jpg" height="200px">
            <img src="~assets/images/cat.png" alt="">
        </div>
        <div>
            img+js(attrs):
            <img :src="jiaban" height="200px">
            <img :src="cat" alt="">
            <ul>
                <li>{{jiaban}}</li>
                <li>{{cat}}</li>
            </ul>
        </div>
        <div class="css-bg">
            img+css(background-images):
            <span class="css-bg__1"></span>
            <span class="css-bg__2"></span>
        </div>
        <div>
            static:
             <img src="static/images/jiaban.jpg" height="200px">
            <img src="static/images/cat.png" alt="">
        </div>
    </div>
</template>

<script>
const jiaban = require('@/assets/images/jiaban.jpg');
const cat =  require('@/assets/images/cat.png');

export default {
    data(){
        return {
            jiaban,
            cat
        }
    }
}
</script>

<style lang="scss">
.css-bg__1,
.css-bg__2{
    display: inline-block;
}
.css-bg__1{
    height: 200px;
    width: 173px;
    background-image: url(~assets/images/jiaban.jpg);
    background-size: contain;
}
.css-bg__2{
    height: 49px;
    width: 49px;
     background-image: url(~assets/images/cat.png);
    background-size: contain;
}
</style>

开发环境截图:

解决.vue文件url引用文件的问题

添加构建路径配置

添加 ExtractTextPluginpublicPath 配置,这里根据实际情况配:

//build/util.js
    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader',
        publicPath: '../../',   
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }

生产环境截图:

解决.vue文件url引用文件的问题

参考链接


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Lean Startup

The Lean Startup

Eric Ries / Crown Business / 2011-9-13 / USD 26.00

更多中文介绍:http://huing.com Most startups fail. But many of those failures are preventable. The Lean Startup is a new approach being adopted across the globe, chan ging the way companies are built and ......一起来看看 《The Lean Startup》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具