简体   繁体   English

如何通过 SCSS + Parcel.js 使用背景图片

[英]How to use background-images with SCSS + Parcel.js

I'm trying to use parcel.js and SCSS to use a background-image, but getting:我正在尝试使用parcel.js和 SCSS 来使用背景图像,但得到:

Uncaught SyntaxError: Unexpected token < 7d6454d814b6ce2e1592d3937c337ef3.js:1未捕获的语法错误:意外令牌 < 7d6454d814b6ce2e1592d3937c337ef3.js:1

Or at times getting:或者有时得到:

Cannot read property 'js' of null无法读取 null 的属性“js”

Here is my scss:这是我的 scss:

.hero{
  background-image: url('../../images/2ndpaper.jpg');
}

Here is my js:这是我的js:

import "../styles/index.scss";
import paperbg from "../images/2ndpaper.jpg";

Here is my package.json:这是我的 package.json:

 {
  "name": "development",
  "version": "1.0.0",
  "description": "CB PC - Web Development",
  "main": "index.js",
  "scripts": {
    "dev": "parcel src/index.html",
    "build": "parcel build src/index.html --out-dir prod"
  },
  "keywords": [
    "client",
    "website"
  ],
  "author": "Designs by Harp",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.3.0",
    "parcel-bundler": "^1.11.0"
  },
  "dependencies": {
    "bootstrap-4-grid": "^2.4.1",
    "gsap": "^2.0.2",
    "jshint": "^2.10.1",
    "sass": "^1.17.2"
  }
}

What could be wrong?有什么问题吗?

我会添加新的 npm 脚本,尝试添加“--public-url ./”,因为他们在Parcel 文档中建议

"build2": "parcel build src/index.html --public-url prod"

May be this is late answer but the background image with Parcel.js and SCSS needs to be used in the way可能这是迟到的答案,但需要使用带有 Parcel.js 和 SCSS 的背景图像

background-image: image-set(url("path/to/image"));

This worked for me...这对我有用...

same answer for this question Parcel JS - not wrapping background-image url in single quotes这个问题的相同答案Parcel JS - not wrapping background-image url in single quotes

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

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