Mag*_*ick 5 javascript css vuejs2 nuxt.js
我使用 Nuxt.js,并有一个自定义组件。
该组件在使用 css 设置背景图像的组件中具有 css。
我尝试了以下操作,但是在运行时出现错误。错误是:
invalid expression: Invalid regular expression flags in
Run Code Online (Sandbox Code Playgroud)
成分
<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>
Run Code Online (Sandbox Code Playgroud)
Mag*_*ick 11
我在https://github.com/nuxt/nuxt.js/issues/2123上找到了答案。
基本上,在组件中做:
<div :style="{ backgroundImage: `url(${backgroundUrl})` }">Content with background here</div>
Run Code Online (Sandbox Code Playgroud)
url('~@/assets/autumn-tree.jpg')
我犯了同样的错误,认为这是一个 nuxtjs 问题。Webpack 使用语法来解析资产。
~ 强制 webpack 将请求视为模块请求。然后@从根开始。
小智 6
这也是使用 require 和 url 组合来解析资产的另一个示例。
<b-col cols="8" class="hj_projectImage justify-content-center text-center" :style="{backgroundImage: `url(` + require(`~/assets/ProjectPictures/${this.ProjectPicture}`) + `)`}">
</b-col>
Run Code Online (Sandbox Code Playgroud)
小智 5
<template>
<div>
<div class="backgroundImage" :style="{ backgroundImage: `url(${backgroundImagePath})` }">
</div>
</template>
<script>
import backgroundImagePath from '~/assets/image.jpeg'
export default {
data() {
return { backgroundImagePath }
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24314 次 |
最近记录: |