• webpack打包静态资源和动态资源


    1.对于静态引用的资源:
    <img src = "static/modelname/imgname.png">
    // 修改为下面的写法
    <img src = "../../../static/modelname/imgname.png">
     
    2.不准在内联内显试的引用图片
    // 禁止出现下面写法
    <div style = "background: src(...)"></div>
     
    3.动态引用的图片
    <img ref = 'test'></img>
    this.$refs.test.src = require('../../static/httpdemo/111.png')
    
    <div ref = 'test'></div>
    this.$refs.test.style.background = 'url(' + require('../../static/httpdemo/111.png') + ')'
    
    // webpack会将../../../static/httpdemo/下所有图片打包。
    <img :src = "require('../../../static/httpdemo/' + id)" v-show="id"></img>
    <div :style = "'background: src(' + require('../../../httpdemo' + id) + ')'"></div>
     
    <template>
      <div>
        <zy-header :headerTitle="$route.meta.title" :rightShow="true"></zy-header>
        <div class="container">
          // 通过:src设置图片路径
          // 如果在页面初始化时或者在操作过程中将imgName值赋为'',这时页面会显示找不到图片,最好加上v-show。
          <img ref="test" class="test" :src="img" v-show="imgName"></img>
          // 通过:style设置背景图路径
          <div class="test" :style="backgroundimg"></div>
        </div>
      </div>
    </template>
    
    <script>
      import ZyHeader from '../../components/ZyHeader'
      export default {
        data () {
          return {
            imgName: '111.png',    // 图片名初始化,后面通过修改imgName的值动态更换图片
            bgImgName: '111.png'   // 背景图初始化
          }
        },
        components: {
          ZyHeader
        },
        computed: {
          // 图片
          img () {
            return this.imgName ? require('../../../static/httpdemo/' + this.imgName) : ''
          },
          // 背景图
          backgroundimg () {
            return this.imgName ? {
              backgroundImage: 'url(' + require('../../../static/httpdemo/' + this.bgImgName) + ')',
              backgroundRepeat: 'no-repeat',
              backgroundSize: '25px auto'
            } : {}
          }
        }
      }
    </script>
    <style scoped>
      .test{
        100px;
        height:100px;
      }
    </style>
     
  • 相关阅读:
    Python的优点和缺点
    如何在sed中使用变量方法及其简单
    shell脚本练习,创建数据文件注册用户并将用户信息存入文件内,用于模拟登录时使用
    shell脚本常用参数与格式
    运维方向和运维需要掌握的基本面
    linux系统awk命令精解
    数组去重方式
    原生js封装cookie获取、设置及删除
    sublime 浏览器快捷键设置
    background-image实现border效果及多图png如何实现background-size为原图一半
  • 原文地址:https://www.cnblogs.com/liululin/p/8177064.html
Copyright © 2020-2023  润新知