Angular 10 在从不同工作区渲染组件时使用库时无法读取 null 的属性“bindingStartIndex”

Jan*_*Mod 0 typescript angular-module angular angular-library angular-ivy

我创建了一个 Angular-Library,它在我的 App-Workspace 之外。结果是我有两个不同的工作区。

我的第一种方法是构建我的库并将/dist文件夹与我的应用程序链接起来。这在 ng serve 上效果不佳,但无论如何我在渲染我的 Library-Component-Templates 时遇到了问题。

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'bindingStartIndex' of null
TypeError: Cannot read property 'bindingStartIndex' of null
[...]
Run Code Online (Sandbox Code Playgroud)

在做我的第一次研究时,我发现了这个 github 问题帖子

基本上给定的解决方案是在我的 tsconfig.json 中添加我的库public-api.ts中的路径,该路径可以导入到我的应用程序源中。像这样:

    "paths": {
    "@app/*": ["src/app/*"],
      "@core": ["src/app/@core"],
    "@core/*": ["src/app/@core/*"],
    "@shared": ["src/app/@shared"],
    "@shared/*": ["src/app/@shared/*"],
      "@env/*": ["src/environments/*"],
      "@myLibrary/*": ["../myLibrary/projects/myLibrary/src/public-api"]
  }
Run Code Online (Sandbox Code Playgroud)

不知何故,我在渲染模板时仍然遇到同样的问题。

因此,我的最后一种方法只是直接从我的 app.module.ts 导入我的 lib-Component

import { TestComponent } from '../../../../myLibrary/projects/myLibrary/src/lib/components/testComponent/test.component';
   @NgModule({
     imports: [
       FlexLayoutModule,
       CelNgZuiModule,
       CommonModule
   ],
   declarations: [FactoryModelComponent, TestComponent,]
   })
Run Code Online (Sandbox Code Playgroud)

结果是一样的。渲染模板时我仍然遇到相同的错误。这种方法目前真的让我感到困惑。我的意思是我只是从另一个位置导入了一个 .ts-File。使用我的应用程序中的组件或从我的库中注入服务工作正常。

测试组件

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'cel-test',
   template: '<p> should work fine </p>',
})
export class TestComponent implements OnInit {

constructor() {
    console.log("it is working");
}

ngOnInit() {
}

}
Run Code Online (Sandbox Code Playgroud)

App-angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "mes-demo": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "preserveSymlinks": true,
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/apple-touch-icon.png",
              "src/robots.txt",
              "src/manifest.webmanifest",
              "src/assets"
            ],
            "styles": [
              "src/main.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": true,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ],
              "serviceWorker": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            },
            "ci": {
              "progress": false
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "mes-demo:build",
            "hmr": true,
            "hmrWarning": false
          },
          "configurations": {
            "production": {
              "browserTarget": "mes-demo:build:production",
              "hmr": false
            },
            "ci": {
              "progress": false
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "mes-demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "scripts": [],
            "styles": [
              "src/main.scss"
            ],
            "assets": [
              "src/favicon.ico",
              "src/apple-touch-icon.png",
              "src/robots.txt",
              "src/manifest.webmanifest",
              "src/assets"
            ]
          },
          "configurations": {
            "ci": {
              "progress": false,
              "watch": false
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "mes-demo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "mes-demo:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "mes-demo"
}

Run Code Online (Sandbox Code Playgroud)

小智 7

我遇到了同样的问题并按如下方式解决了

我在angular.json文件的“options”中插入了“preserveSymlinks”属性,如下

"projects.project-name.architect.build.options.preserveSymlinks": true
Run Code Online (Sandbox Code Playgroud)

然后我将它添加到tsconfig.json文件中

paths: {
   "@angular/*": [
      "./node_modules/@angular/*"
   ],
}
Run Code Online (Sandbox Code Playgroud)

我希望我帮助了某人。