简体   繁体   English

Vue3 css 背景图像注入

[英]Vue3 css background-image injection

Constructing from my previous question , I would like now to not only inject a variable to my scoped css, but to inject an url image.从我之前的问题构建,我现在不仅要向我的作用域 css 注入一个变量,还要注入一个 url 图像。 I've tried several combinations, but none seem to work:我尝试了几种组合,但似乎没有一个有效:

<template>
    <header class="header">
    </header>
</template>
<script>
import { ref, defineComponent, computed } from 'vue'
export default defineComponent({
    components: { },
    setup() {
        const cssVars = ref()
        cssVars.value = {myImage: '"hero-small.jpg"'}
        const imageBind = computed(() => '"../../assets/img/hero-small.jpg"')
        return {
            cssVars,
            imageBind,
        }
    },
})
</script>

<style lang="scss" scoped>
.header {
    background-image: url(v-bind(imageBind));
}
</style>

Any tips?有小费吗?

PD: CONFIG FILES: PD:配置文件:

package.json package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.21.0",
    "bootstrap": "^4.5.3",
    "core-js": "^3.6.5",
    "jquery": "^3.5.1",
    "popper.js": "^1.16.1",
    "register-service-worker": "^1.7.1",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-pwa": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-airbnb": "^5.1.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^7.13.0",
    "eslint-plugin-import": "^2.22",
    "eslint-plugin-vue": "^7.1.0",
    "node-sass": "^5.0.0",
    "sass-loader": "^10.1.0"
  }
}

jsconfig.json jsconfig.json

{
    "include": ["./src/**/*"],
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "@/*": ["./src/*"]
        }
    }
}

.eslintrc.js .eslintrc.js

module.exports = {
    root: true,
    env: {
        node: true,
    },
    extends: [
        'plugin:vue/vue3-essential',
        '@vue/airbnb', // https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb-base/rules
    ],
    parserOptions: {
        parser: 'babel-eslint',
    },
    rules: {
        'global-require': 0,
        'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
        'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',

        // https://eslint.org/docs/rules/prefer-destructuring
        'prefer-destructuring': ['error', { object: true, array: false }],

        // https://eslint.org/docs/rules/max-len
        'max-len': [
            'warn',
            {
                code: 120,
                tabWidth: 4,
                ignoreUrls: true,
                ignoreComments: false,
                ignoreRegExpLiterals: true,
                ignoreStrings: true,
                ignoreTemplateLiterals: true,
            },
        ],

        // https://eslint.org/docs/rules/semi
        semi: ['off', 'never'],

        // https://eslint.org/docs/rules/indent
        indent: ['warn', 4],

        // https://eslint.org/docs/rules/no-underscore-dangle
        'no-underscore-dangle': ['off'],

        // https://eslint.org/docs/rules/class-methods-use-this
        'class-methods-use-this': ['off'],

        // https://eslint.vuejs.org/rules/no-multiple-template-root.html
        'no-multiple-template-root': ['off'],
    },
    overrides: [
        {
            files: ['**/__tests__/*.{j,t}s?(x)', '**/tests/unit/**/*.spec.{j,t}s?(x)'],
            env: {
                jest: true,
            },
        },
    ],
}

babel.config.js babel.config.js

module.exports = {
    presets: ['@vue/cli-plugin-babel/preset'],
}

Try out to require the image:尝试require图像:

const imageBind = computed(() => require("../../assets/img/hero-small.jpg"))

then:然后:

<style lang="scss" scoped>
.header {
    background-image: url(v-bind(imageBind));
}
</style>

As an alternative to setting the variable dynamically as a CSS class via v-bind() , it's also possible to bind as inline style :作为通过v-bind()将变量动态设置为 CSS class的替代方法,也可以将其绑定为内联样式

<template>
    <header :style="{backgroundImage: `url('$imageUrl')`}"></header>
</template>

See https://vuejs.org/guide/essentials/class-and-style.html#binding-inline-styleshttps://vuejs.org/guide/essentials/class-and-style.html#binding-inline-styles

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM