【问题标题】:Angular : unable to load component from library : Cannot read properties of null (reading '11')Angular:无法从库中加载组件:无法读取 null 的属性(读取“11”)
【发布时间】:2022-01-25 06:37:30
【问题描述】:

在我的 Angular 应用下

我正在尝试加载我从本地角度库中获取的组件

我的组件(来自库)如下所示:

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

@Component({
  selector: 'lib-ada-headband-lib',
  templateUrl: './ada-headband-lib.component.html',
  styleUrls: ['./ada-headband-lib.component.scss']
})
export class AdaHeadbandLibComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

它的视图 html 看起来像这样:

<span>
  ada-headband-lib works!
</span>

在我的主要 Angular 项目下

我是通过将 app.module 中的库作为模块导入来从库中获取的

然后,我把它放在我的 app.component.html 下,如下所示:

  <section>
    <lib-ada-headband-lib></lib-ada-headband-lib>
  </section>

但加载失败并抛出此错误:

core.js:6479 ERROR TypeError: Cannot read properties of null (reading '11')
    at Object.Jv (core.js:14869:1)
    at template (ada-headband-lib.component.ts:8:14)
    at bc (core.js:9599:1)
    at id (core.js:9402:1)
    at Za (core.js:10683:1)
    at core.js:9267:1
    at id (core.js:9427:1)
    at L0.createEmbeddedView (core.js:23026:1)
    at F0.createEmbeddedView (core.js:23157:1)
    at Fn._updateView (common.js:3463:1)

好像不重要,请问如何调整??

【问题讨论】:

  • 您是否在 (ada-headband-lib.component.ts:8:14) 处检查了您的库?
  • @Gopal:我已经附上了代码,很简单:export class AdaHeadbandLibComponent implements OnInit {

标签: angular angular-template angular-library


【解决方案1】:

解决方案是将选项 "preserveSymlinks":true 添加到 angular.json

"projects": {
    "myapp": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "preserveSymlinks": true,
       ...
  
        

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2018-09-02
    • 2019-07-03
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多