nuxt.js-如何动态设置CSS背景图像

JavaScript Vue.js

LGil

2020-04-07

我正在使用Nuxt.js,并具有一个自定义组件。

该组件中的css使用css设置背景图像。

我尝试了以下操作,但是运行此程序时出现错误。错误是:

 invalid expression: Invalid regular expression flags in

零件

<template>
  <section class="bg-img hero is-mobile  header-image" v-bind:style="{ backgroundImage: 'url(' + image + ')' }">
    <div class="">
      <div class="hero-body">
        <div class="container">
          <h1 class="title">
            {{ result }}
          </h1>
          <h2 class="subtitle ">
            Hero subtitle
          </h2>
        </div>
      </div>
    </div>

</section>
</template>

<script>

export default {
  props: ['result', 'image']
}
</script>


<style>



.bg-img {
        background-image: url(~/assets/autumn-tree.jpg);
        background-position: center center;
        background-repeat:  no-repeat;
        background-attachment: fixed;
        background-size:  cover;
        background-color: #999;

 }

</style>

第4134篇《nuxt.js-如何动态设置CSS背景图像》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Tony凯 2020.04.07

我在https://github.com/nuxt/nuxt.js/issues/2123上找到了答案

基本上,在组件中执行以下操作:

<div :style="{ backgroundImage: `url(${backgroundUrl})` }">Content with background here</div>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android