使用templateUrl时,Angular编译器返回错误:为组件指定的模板不是字符串

时间:2019-07-18 08:02:09

标签: javascript angular

在我的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 dev构建。

对于除app-root以外的每个组件,templateMeta中的template属性的类型为Module而不是string

开发工具: See Image of Dev Tools Log

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"
}

extra-webpack.config.js

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

1 个答案:

答案 0 :(得分:0)

我通过从package.json中的依赖项中删除了raw-loader@3.0.0来解决了这个问题。现在,我使用角度生成器提供的原始加载器。