gpt4 book ai didi

angular - 使用 ng-packagr 组件时为 "No provider for ViewContainerRef"

转载 作者:行者123 更新时间:2023-12-02 21:25:44 29 4
gpt4 key购买 nike

我使用 Angular cli nggeneratelibrary 创建了一个组件,以及在模块内使用 *ngIf 的组件。

在我的主项目中成功构建并安装库后,当我尝试使用该组件时,出现 Noprovider for ViewContainerRef 错误。

版本:

@angular/cli 版本:“~7.0.6”,

@Angular/* 版本:“~7.0.0”

错误:

ERROR Error: StaticInjectorError(AppModule)[NgIf -> ViewContainerRef]: StaticInjectorError(Platform: core)[NgIf -> ViewContainerRef]: NullInjectorError: No provider for ViewContainerRef!

组件:

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

@Component({
selector: 'al-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.scss']
})
export class CardComponent implements OnInit {

@Input() header: string;
@Input() text: string;

constructor() { }

ngOnInit() {
}

}

模板:

<div *ngIf="header">

</div>

模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CardComponent } from './card.component';

@NgModule({
declarations: [CardComponent],
imports: [
CommonModule
],
exports: [CardComponent],
})
export class CardModule { }

最佳答案

我已经发现问题了,通过将 "preserveSymlinks": true 添加到我的主项目的 angular.json 文件中解决了这个问题。

"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
...

这是我找到解决方案的地方: https://github.com/angular/angular-cli/issues/10896

关于angular - 使用 ng-packagr 组件时为 "No provider for ViewContainerRef",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53833985/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com