您好,我想知道如果组件已在 AppModule 中声明,是否可以在不再次导入的情况下使用该组件。由于我有 10 个或更多页面/组件要使用,因此很难导入每个页面/组件并进行跟踪。
这是我的app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
我想使用主页而不重新导入它。因为我有很多页面要处理它。
other-page.ts
import { Component } from '@angular/core';
@Component({
selector: 'page-home',
templateUrl: 'login.html',
providers: [LoginService]
})
export class LoginPage {
constructor(){ }
//Call HomePage here without having to import it again.
}
最佳答案
如果您在代码中的某处提到一个类,则必须导入它,否则它不会被识别为该类,从而使代码无效或至少不会执行您期望的操作。
文件顶部的导入与 Angular 完全无关,它们是 TypeScript 导入。 @NgModule()
中的导入是 Angular 导入,其用途与 TS 导入完全不同。
因此,向 @NgModule()
添加组件不会使其他导入过时。
关于angular - 使用组件而不导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42502748/