前端vue-cli项目中使用img图片和background背景图的几种方法

前端中背景图片极其常用,但是很容易出现各种问题.

一种是脚手架本身资源引用方式的问题,如指定静态资源文件夹.

一种是图片资源引入方式,有时候使用绝对或者相对路径会导致错误.

css方法

正常使用background属性即可.

如有问题,应把图片资源放入static静态资源文件夹,不是assets之类的其他文件夹.

<div class=\"bgImg\"></div>
<style>
.bgImg{
  background-image:url(\"@/../static/images/logo.png\")
}
</style>

import方法

使用import导入背景图片

import bgImg from \"@/../static/images/logo.png\"
export default {
  name: \'App\',
  data () {
    return {
      bgImg: bgImg,
    }
  }
}

使用内联样式

<div :style=\"{backgroundImage:\'url(\'+bgImg+\')\'}\"></div>

require方法

使用require获取图片

export default {
  name: \'App\',
  data () {
    return {
      bgImg: require(\"@/../static/images/logo.png\"),
    }
  }
}

赋值为img的src

<img :src=\"bgImg\" />

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

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容