如何将组件导入到 Angular 中的服务类

Sub*_*ham 5 typescript angular-ui-router angular

我正在尝试创建一个 Angular 对话框。为此,我将一个组件导入到 Service 类中,但出现以下错误。我正在使用不同的模块并在该模块上添加了一个入口组件

错误:未找到 NewPasswordComponent 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

新密码/新密码.module.ts

   import { NewPasswordRoutingModule  } from './newpassword- 
   routing.module';
   import { NewPasswordComponent  } from './newpassword.component';

   export * from './newpassword.component';

    @NgModule({
    imports: [
    CommonModule,
    NewPasswordRoutingModule

    FlexLayoutModule.withConfig({addFlexToParent: false})
   ],
    exports: [
    NewPasswordComponent
    ], 
 declarations: [NewPasswordComponent],
entryComponents: [NewPasswordComponent],

 })
 export class NewPasswordModule {}
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import {AppRoutingModule} from "./app-routing.module";
import { AppComponent } from './app.component';
import {SomeService} from "./shared/someservice.service";


 @NgModule({
  declarations: [
   AppComponent,

   ],
imports: [
  BrowserModule,
 AppRoutingModule,

  ],

 providers: [SomeService],
   bootstrap: [AppComponent]
  })
   export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

共享/someservice.service.ts

   import {MatDialog, MatDialogRef, MAT_DIALOG_DATA,MatDialogConfig} from 
  '@angular/material/dialog';
   import { NewPasswordComponent  } from 
   '../newPassword/newpassword.component';
   @Injectable()
  export class SomeService  {

 constructor(public dialog: MatDialog) {
 }




 LogIn(){


      let dialogRef: MatDialogRef<NewPasswordComponent>;
               const config = new MatDialogConfig();;
              config.role = 'dialog';
               config.width = '60%';
                config.data = { newpassword:  that.newpassword };
             dialogRef = that.dialog.open(NewPasswordComponent, config);
 console.log("Dialog Start"+email);

   dialogRef.afterClosed().subscribe(result => {
   that.newpassword = result;
     console.log("Dialog closed"+result);
  });

 }


 }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误。我在密码模块中添加了入口组件,但仍然收到此错误

错误:未找到 NewPasswordComponent 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

Dav*_*vid 1

我认为您所观察到的行为在github 问题中进行了描述:延迟加载模块的入口组件在根级注入器中不可用。

因此,您需要在您的NewPasswordModule而不是在AppModule.

如果您正在寻找更通用的方法(假设您想在许多延迟加载模块中使用该服务),您可以看看这个建议的解决方案,它创建一个通用服务,然后在延迟加载功能中继承并提供该服务模块。