zoukankan      html  css  js  c++  java
  • vue 打包后 static 中图片路径不对的问题

    在我们准备打包项目之前,需要考虑的一个问题是:【我们的项目最终是要部署在根目录还是子目录】

    如果是部署在 根目录,并不会出现什么问题,因为你的资源文件放在 static 中,然后你在引用的时候,都是 /static/images/a.png、/static/images/login-bg.jpg 这样的路径来引用(不管是 css 中,还是 template 中),这样是不会有任何问题的。

    大家看到我这个项目的访问路径,应该就知道,我这是属于 【子目录】 部署的情况。

    那么在 子目录 部署的时候,就有可能出现本文要说的这个问题 【vue 打包后 static 中图片路径不对的问题】所以,本文后续内容的前置条件就是 子目录 部署。

    【重点内容开始】

    首先,你如果要部署在子目录的话,那么,你的 config/index.js 配置中,需要将 assetsPublicPath: '/' 改成 assetsPublicPath: './' 

    示例代码 。

     build: {
        // Template for index.html
        index: path.resolve(__dirname, '../dist/index.html'),
    
        // Paths
        assetsRoot: path.resolve(__dirname, '../dist'),
        assetsSubDirectory: 'static',
        assetsPublicPath: './',
    
        /**
         * Source Maps
         */
    
        productionSourceMap: true,
        // https://webpack.js.org/configuration/devtool/#production
        devtool: '#source-map',
    
        // Gzip off by default as many popular static hosts such as
        // Surge or Netlify already gzip all static assets for you.
        // Before setting to `true`, make sure to:
        // npm install --save-dev compression-webpack-plugin
        productionGzip: false,
        productionGzipExtensions: ['js', 'css'],
    
        // Run the build command with an extra argument to
        // View the bundle analyzer report after build finishes:
        // `npm run build --report`
        // Set to `true` or `false` to always turn it on or off
        bundleAnalyzerReport: process.env.npm_config_report
      }
    

      

    这一点,应该没什么疑问吧。
    然后这个时候,你的代码中,引入 static 中图片的时候,就不能使用 /static/images/a.png 这种绝对路径的形式; (虽然,你在开发环境,使用这种绝对路径的方式仍然没有任何问题,但是你部署后,就会发现,原本应该请求 xxx.com/aaaa/static/... 下面的资源,实际的请求地址是 xxx.com/static/... 就像我文章开头说的那样)。而是要使用相对路径的形式。=> 示例代码 大致如下:

    <style scoped lang="stylus">
    .login-container
    background: mix(#494166, #424b50) url('../../../static/image/login-bg.jpg') center no-repeat;
    background-size: cover;
    </style>
    

      

    与此同时呢,由于我们在 css 中使用了相对路径来引入图片。那么,打包后,我们的 css 会抽离出来为一个单独的 css 文件,这个时候, css 和 图片 之间的一个相对路径关系就改变了。为了解决这个问题,我们需要在 build/utils.js 中,增加一个 pablicPath 的配置,示例代码 如下:

    if (options.extract) {
    return ExtractTextPlugin.extract({
    use: loaders,
    fallback: 'vue-style-loader',
    publicPath: '../../' // 作用是设置打包过程中提取css的方法
    })
    } else {
    return ['vue-style-loader'].concat(loaders)
    }
    

      



    这个时候,就解决了打包后图片路径404的问题了。

    我们项目中,不光 style 中会有图片的引用,在 template 中同样会有图片的引用。主要分下面三种情况:

    style 标签中 background-image (上面示例中已讲解)
    template 中 img 标签中 src
    template 中 img 标签中 v-bind:src
    template 中 img 标签中 src
    这种情况与 style 标签中 background-image 一样,我们使用相对路径就行了。
    这里 也有一个例子:

    <div slot="header" class="el-card-header">
    <img src="../../../static/image/login-logo.png" alt="">
    <h2 class="login-title">{{$t('login.title')}}</h2>
    </div>
    

      


    【唯一需要值得注意的是,template 中 img 标签中 v-bind:src 将不适用这个规则】

    template 中 img 标签中 v-bind:src
    这里 有一个例子:

    <div class="avatar-wrapper">
    <img class="user-avatar" :src="avatar">
    <div class="username-wrapper">
    <span class="user-name">{{name}}</span>
    <i class="el-icon-caret-bottom"></i>
    </div>
    </div>
    

      



    这里的 :src="avatar" 中的 avatar 是多少呢?

    admin: {
    avatar: './static/image/avatar/0.jpg'
    }
    

    直接使用 src 和 v-bind:src 还是有区别的。

     

    转载自:https://blog.csdn.net/csdn_yudong/article/details/84936130

  • 相关阅读:
    Android开发 使用 adb logcat 显示 Android 日志
    【嵌入式开发】向开发板中烧写Linux系统-型号S3C6410
    C语言 结构体相关 函数 指针 数组
    C语言 命令行参数 函数指针 gdb调试
    C语言 指针数组 多维数组
    Ubuntu 基础操作 基础命令 热键 man手册使用 关机 重启等命令使用
    C语言 内存分配 地址 指针 数组 参数 实例解析
    CRT 环境变量注意事项
    hadoop 输出文件 key val 分隔符
    com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Too many connections
  • 原文地址:https://www.cnblogs.com/fanqiuzhuji/p/12613778.html
Copyright © 2011-2022 走看看