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

Angular 10 Cannot read property 'bindingStartIndex' of null when using Library while rendering component from different workspace

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

我的第一个方法是用我的应用程序构建我的库和 link /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
[...]

在进行我的第一次研究时,我发现了这个 github 问题 post

基本上给出的解决方案是在我的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"]
  }

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

因此,我最后的方法只是直接从 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,]
   })

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

Test.component

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() {
}

}

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

幸运的是我解决了我的问题,但不幸的是我真的不知道如何解决。昨天我删除了我应用程序中的所有 node_module。也许这以某种方式解决了我的问题。

我遇到了同样的问题,解决方法如下

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

"projects.project-name.architect.build.options.preserveSymlinks": true

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

paths: {
   "@angular/*": [
      "./node_modules/@angular/*"
   ],
}

我希望我帮助了某人。

如果您引用当前工作文件夹之外的组件文件路径,或者基本上如果您用于添加组件的语法和结构不正确,也可能会发生这种情况。有时,如果您不小心,您的编辑的帮助是无济于事的。