【问题标题】:Angular compiler returns error when using templateUrl: The template specified for component is not a stringAngular 编译器在使用 templateUrl 时返回错误:为组件指定的模板不是字符串
【发布时间】:2019-11-27 02:36:24
【问题描述】:

在我的 Angular CLI 项目中,当通过 templateUrl 加载模板时,JIT 编译器返回 Uncaught Error: The template specified for component IconComponent is not a string,但内联 template 工作正常。

我正在使用自定义 webpack 配置 (@angular-builders/custom-webpack": "^8.1.0) 运行默认的 cli 开发构建。

对于除app-root 之外的每个组件,templateMeta 中的模板属性都是Module 类型而不是string

开发工具:

icon.component.ts

@Component({
  selector: 'cmp-icon',
  templateUrl: './icon.component.html',
})

icon.component.html

<span *ngIf="!hasContainer" [ngClass]="'.c-icon'"></span>

Angular CLI 信息:

Angular CLI: 8.1.2
Node: 12.6.0
OS: darwin x64
Angular: 8.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.801.2
@angular-devkit/build-angular     0.801.2
@angular-devkit/build-optimizer   0.801.2
@angular-devkit/core              8.1.2
@angular-devkit/schematics        8.1.2
@schematics/angular               8.1.2
@schematics/update                0.801.2
rxjs                              6.5.2
typescript                        3.4.5
webpack                           4.36.1

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "fg": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "cmp",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "outputPath": "dist/fg",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            },
            "indexTransform": "./index-html.transform.js",
            "assets": ["src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": [],
            "preserveSymlinks": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "browserTarget": "fg:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "fg:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "fg:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": ["src/styles.scss"],
            "scripts": [],
            "assets": ["src/favicon.ico", "src/assets"]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
            "exclude": ["**/node_modules/**"]
          }
        }
      }
    },
    "fg-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "fg:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "fg:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": ["**/node_modules/**"]
          }
        }
      }
    }
  },
  "defaultProject": "fg"
}

额外的webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.snippet.*\.html$/,
        use: ['html-loader'],
      },
    ],
  },
};

【问题讨论】:

  • 请提供更多信息。

标签: javascript angular


【解决方案1】:

我通过从 package.json 的依赖项中删除 raw-loader@3.0.0 解决了这个问题。我现在使用 angular builder 提供的 raw-loader。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2017-04-09
    • 2019-02-25
    • 1970-01-01
    • 2019-09-02
    • 2019-07-09
    • 1970-01-01
    相关资源
    最近更新 更多