简体   繁体   English

如何链接背景图像 [vue cli]

[英]How can I link background-image [vue cli]

when I'm trying to run this code below, I have a problem.当我尝试在下面运行此代码时,我遇到了问题。 It runs, but I can't see any images and it writes that url (unknown).它运行,但我看不到任何图像,它写道 url(未知)。 How can I solve it.我该如何解决。 All images definately exists.所有图像肯定存在。

 <template> <div class="slider"></div> </template> <style lang="scss" scoped>.slider { width: 100%; height: 100%; background-image: url("../assets/images/slides/2160/0.jpg"); } </style> <script> export default { name: "Slider", }; </script>

Thanks it works — [js vue.js]谢谢它有效- [js vue.js]

 backgroundImage: `url(${require('../assets/images/slides/2160/0.jpg')})`

but what should I do if my path already use ${} for example但是如果我的路径已经使用 ${} 例如我该怎么办

 "../assets/images/slides/${this.$assetsResolution}/${i}.jpg"

try doing this:尝试这样做:

<template>
  <div :style="bgImg"></div>
</template>

<script>
  export default {
    data() {
      return {
        bgImg: {
          backgroundImage: `url(${require('../assets/images/0.jpg')})`
        }
      }
    }
  }
</script>

As Mentioned, use a field in data to get the image and link it to your dom.如前所述,使用数据中的字段来获取图像并将其链接到您的 dom。 The template:模板:

  <div
class=" login d-flex justify-content-center align-items-center"
:style="{ background: ` center/cover url(${image})` }"

the data object:数据 object:

  data() {
return {
  image: require('@/path/to/picture/yourimagehere.jpeg'),
};

}, },

The best way is to load it in beforeMount lifecycle hook最好的方法是在beforeMount生命周期钩子中加载它

beforeMount(){
    const styles = {
      "background": url('../assets/images/0.jpg'),
      "background-size": "cover",
      "background-repeat": "no-repeat",
    };
    Object.assign(document.body.style, styles);
}
<div class=" search-box car-rims" :style="{ backgroundImage: `linear-gradient( rgba(255, 255, 255, 1.45), rgba(255, 255, 255, 0.45) ), url( ${require('@/assets/your-image.jpg')})`, backgroundSize: 'cover', backgroundRepeat: 'norepeat', }" > </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue - 如何在背景图片网址中使用@ - Vue - how to use @ in background-image url 创建选项时如何更改选项的背景图像? - How can I change the background-image of an option while creating it? 如何将 Javascript 元素放入 CSS 背景图像 - How can I put Javascript element into CSS background-image 如何绑定CSS background-image属性? - How can I bind the CSS background-image property? Rivetjs | 如何将图片链接绑定到style =“ background-image:url()” - Rivetjs | How to bind image link to style=“background-image:url()” 如何在 vue-cli 3 中设置 CSS 背景? - How can I setting CSS background in vue-cli 3? Vue:绑定背景图像样式无效 - Vue: Binding background-image style not working 如何使用链接更改背景图片 - How can I change the background image with a link 为什么 backgroundimage 在 chrome 中不是 cssImageValue? 我如何使用 canvas 处理背景图像但不制作新图像? - why backgroundimage is not cssImageValue in chrome? how can i use canvas deal with background-image but not make a new image? 在jQuery中,如何在不使用JavaScript对图像的URL进行硬编码的情况下设置元素的背景图像? - In jquery, how can I set the background-image of an element without hardcoding the url of the image in my javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM