【问题标题】:Angular 6 - build with assets not foundAngular 6 - 使用未找到的资产构建
【发布时间】:2019-04-19 10:24:00
【问题描述】:

我正在尝试创建我的 Angular 项目的构建。 我正在将 scss 与这样的资产一起使用:

.ciao {
  background-image: url("../../assets/images/bck--registration-company.png");
}

运行此代码:

ng build --prod --configuration=coll --base-href ./

我在 dist 文件夹中,在 index.html 的同一目录中,文件“bck--registration-company.png”和“assets”文件夹里面还有另一个“bck--registration-company.png” ”。我想使用 assets 文件夹中的资源。

我尝试更改基本 href、相对和绝对图像路径,但没有。 示例:

.background--image-carousel2{
  background-image: url("/assets/images/carousel2.jpg");
}

使用此路径,请求 URL)是:

Request URL: http://localhost:63342/assets/images/carousel2.jpg

这是错误的,因为它可能是:

http://localhost:63342/project/dist/name_project/assets/images/bck--registration-company.png

在我的 angular.json 中:

"architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist/name_project",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.app.json",
        "assets": [
          "src/assets"
        ],
        "styles": [
          "src/styles.scss"
        ],
        "scripts": [

        ]
      },

我该如何解决这个问题? 谢谢。

【问题讨论】:

  • 试试这个url("./assets/images/carousel2.jpg");
  • 这里也一样,错误:NonErrorEmittedError: (Emitted value instead of an instance of Error) CssSyntaxError

标签: angular typescript sass


【解决方案1】:

试试这个方法

Angular 可以直接访问 assets 文件夹。

.ciao {
   background-image: url("assets/images/bck--registration-company.png");
}

然后进行构建

ng build --prod 

然后将整个系统路径放入 dist/index.html 文件中

在 base url 中设置项目文件夹的整个路径。

【讨论】:

  • 我在构建时出错:ERROR in Module build failed (from ./node_modules/sass-loader/lib/loader.js): background-image: url(assets/images/bck--registration-company.png"); ^ Invalid CSS after "...tration-company": expected expression (e.g. 1px, bold), was '.png");' in C:\Users\t.ruscica\workspace\ws_svn_agos\project\project_name\src\app\landing\landing.component.scss (line 3, column 64)
  • 试试background-image: url(./assets...
  • NonErrorEmittedError:(发出的值而不是错误的实例)CssSyntaxError。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2011-06-06
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多