gpt4 book ai didi

angular - inject() 必须从注入(inject)上下文中调用

转载 作者:太空狗 更新时间:2023-10-29 17:09:25 25 4
gpt4 key购买 nike

我正在尝试将我的 Angular 应用程序导出为 npm 模块以供其他应用程序使用,但遇到了一些困难。我无法在 Internet 上的其他任何地方找到此错误,而且我已经无能为力了。

我遵循了本教程:https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e

我使用 ng-packagr 将我的应用导出为 npm 模块。我可以从准系统测试应用程序的本地文件夹成功安装它,但无法让它显示我的应用程序。

错误:

    AppComponent.html:1 ERROR Error: inject() must be called from an injection context
at inject (core.js:1362)
at ChangeStackService_Factory (template-wiz.js:2074)
at _callFactory (core.js:8223)
at _createProviderInstance (core.js:8181)
at resolveNgModuleDep (core.js:8156)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:8849)
at resolveDep (core.js:9214)
at createClass (core.js:9094)
at createDirectiveInstance (core.js:8971)
at createViewNodes (core.js:10191)

template-wiz.module.ts(正在导出的模块)

    import { NgModule, ChangeDetectorRef, ComponentFactoryResolver } from '@angular/core';
import { TemplateWizComponent } from './template-wiz.component';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BlockListDirective } from './Directives/block-list.directive';
import { TemplateItemsDirective } from './Directives/template-items.directive';
import { ContextMenuComponent, SeperatorComponent, DragBoxComponent, SnapLineComponent, PropertiesComponent, ToolboxComponent } from './Components'
import { AddressBlockComponent, TextBlockComponent, ImageBlockComponent, DataBlockComponent } from './Data-Blocks';
import { BlockFactoryService, BlockRegistryService, DisplayInfoService, MouseClickService, SavingService, SnapService, TextHelperService, UserModeService } from './Services';
import { PageContextMenuComponent } from './Components/page-context-menu/page-context-menu.component';
import { CamelToWordsPipe } from './Pipes/camel-to-words.pipe';
import { PdfPublisherService } from './Services/pdf-publisher/pdf-publisher.service';
import { GradientBlockComponent } from './Data-Blocks/gradient-block/gradient-block.component';
import { PropToTypePipe } from './Pipes/prop-to-type.pipe';
import { ShapeBlockComponent } from './Data-Blocks/shape-block/shape-block.component';
import { CommonModule } from '@angular/common';
import { ModuleWithProviders } from '@angular/compiler/src/core';


@NgModule({
imports: [
CommonModule,
FormsModule,
HttpClientModule
],
entryComponents: [
AddressBlockComponent,
ContextMenuComponent,
DragBoxComponent,
GradientBlockComponent,
ImageBlockComponent,
PageContextMenuComponent,
SeperatorComponent,
ShapeBlockComponent,
SnapLineComponent,
TextBlockComponent
],
declarations: [
TemplateWizComponent,
DataBlockComponent,
AddressBlockComponent,
SeperatorComponent,
BlockListDirective,
TemplateItemsDirective,
ImageBlockComponent,
TextBlockComponent, DragBoxComponent,
SnapLineComponent,
ToolboxComponent,
PropertiesComponent,
ContextMenuComponent,
PageContextMenuComponent,
GradientBlockComponent,
CamelToWordsPipe,
PropToTypePipe,
ShapeBlockComponent
],
providers: [
BlockFactoryService,
BlockRegistryService,
DisplayInfoService,
MouseClickService,
SavingService,
SnapService,
TextHelperService,
UserModeService,
PdfPublisherService
],
//bootstrap: [TemplateWizComponent],
exports: [
TemplateWizComponent
]
})
export class TemplateWizModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: TemplateWizModule,
providers: [
ComponentFactoryResolver
]
}
}
}

app.module.ts(使用我的模块的基本测试应用程序)

    import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { TemplateWizModule } from 'template-wiz';

@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
FormsModule,
TemplateWizModule.forRoot(),
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

如有任何帮助或指点,我们将不胜感激,谢谢。

最佳答案

使用库时使用 npm link 似乎有问题。

解决方案:在客户端项目的 angular.json 文件中使用 projects.$name.architect.build.options.preserveSymlinks: true,而不是库。

更多信息:https://github.com/angular/angular/issues/25813

关于angular - inject() 必须从注入(inject)上下文中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485868/

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