Angular 使用组件而不导入

Angular 使用组件而不导入,angular,typescript,ionic2,Angular,Typescript,Ionic2,您好,我想知道如果一个组件已经在AppModule中声明,是否可以在不再次导入的情况下使用它。因为我有10个或更多的页面/组件要使用,并且很难导入每个页面和跟踪 这是我的app.module.ts import { NgModule, ErrorHandler } from '@angular/core'; import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular'; import { MyApp } from

您好,我想知道如果一个组件已经在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 {}
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.
}
我想使用主页,而无需重新导入。因为我有这么多的页面来处理它

其他页面。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 {}
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()
中的导入是角度导入,与TS导入完全不同


因此,将组件添加到
@NgModule()
不会使其他导入过时。

要在源代码(ts文件)中使用
组件
,您需要
导入它,以便
类型脚本
识别它。
如果您只想在
模板中使用它
,那么它只需要被
Angular
框架识别,不需要导入

要让
Angular
了解您的
组件
,您需要
NgModule
中声明它。此
NgModule
中的每个
组件
都将知道您的
组件


要让其他
NgModule
s知道您的
组件
,您需要
从声明
NgModule
导入
NgModule
中的
NgModule
中导出它。

谢谢,所以如果在该组件中调用这些类,我仍然需要始终导入它们。这可能有点多余,但要感谢澄清。是的,如果您在代码中使用类名,您总是需要TS导入,这是没有办法的,Angular需要组件导入来将组件与模块关联。但是typescript导入的最大问题是您必须知道您确切需要什么。例如,您无法在单击事件中动态导入组件,如果您定义了百万个模块,则必须写入百万行导入。。我过去很喜欢angular,现在又回到dojo和其他灵活的javascript框架,这是一种不同的软件开发方式,更像Java。对于更大的应用程序,这被认为是一个优势(静态代码检查、更好的树抖动等)。如果您喜欢JavaScript,我可以想象,这对您来说并不具有吸引力。