如何将组件导入Angular中的Service类

时间:2019-06-21 11:50:17

标签: angular typescript angular-ui-router

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

  

错误:找不到NewPasswordComponent的组件工厂。你是否   将其添加到@ NgModule.entryComponents吗?

newpasword / newpassword.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 {}

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

shared / 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);
  });

 }


 }

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

  

错误:找不到NewPasswordComponent的组件工厂。你是否   将其添加到@ NgModule.entryComponents吗?

2 个答案:

答案 0 :(得分:1)

NewPasswordModule导入到AppModule模块

imports: [
  BrowserModule,
  AppRoutingModule,
  NewPasswordModule
 ],

答案 1 :(得分:0)

我认为您正在观察的行为描述为github issue:延迟加载的模块的入口组件在根级别注入器上不可用。

因此,您需要在NewPasswordModule中而不是AppModule中提供使用入口组件的服务。

如果您正在寻找一种更通用的方法(例如,您想在许多延迟加载的模块中使用该服务),则可以看看此suggested solution,它创建了一个通用服务,该服务随后被继承并提供  延迟加载的功能模块。

相关问题