繁体   English   中英

Angular 2 \\ TypeScript中export关键字的确切含义是什么?

[英]What is the exact meaning of export keyword in Angular 2\TypeScript?

我在Angular 2还很陌生 我正在研究如何在Angular应用程序中创建模块,并且我对以下与我正在关注的教程有关的疑问有所疑问。

我的怀疑与路由有关。

因此,在我的示例中定义了AuthModule模块:

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { SigninComponent } from './signin/signin.component';
import { SignupComponent } from './signup/signup.component';
import { AuthRoutingModule } from './auth-routing.module';

@NgModule({
  // Components and directives used by the module:
  declarations: [
    SigninComponent,
    SignupComponent
  ],
  // Import modules used by this features module:
  imports: [
    FormsModule,
    AuthRoutingModule
  ]
})
export class AuthModule {}

并且我定义了相关的rotues配置类:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { ShoppingListComponent } from './shopping-list/shopping-list.component';

const appRoutes: Routes = [
  { path: '', redirectTo: '/recipes', pathMatch: 'full' },
  { path: 'shopping-list', component: ShoppingListComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
})
export class AppRoutingModule {

}

因此,我认为export关键字意味着可以将该类相关的内容导出并在其他地方使用(在这种情况下,我认为是AuthModule类的imports数组)。

是吗? 还是我错过了什么? 出口声明的确切含义是什么?

我不了解它是否与Angular有关,或更笼统地说与TypeScript有关(因为在这里我找到了https://www.typescriptlang.org/docs/handbook/modules.html )。 因此在我看来,这个模块概念并不直接绑定到Angular 2框架,而是一个TypeScript概念,可以用一种灵巧的方式细分我们的代码(然后Angular 2可以使用这种语言的功能)。

是我还是缺少什​​么?

Angular导入/导出和TypeScript导入/导出是两个不同的概念。

TypeScript导入/导出在语言级别进行工作,以使所使用的标识符准确地引用什么。 这与Angular完全无关。

因此,如果使用FormsModule ,就不会有任何歧义,这就是FormsModule的意思。 如果您的代码或任何依赖FormsModule中有多个FormsModule ,则需要通过导入使其含义清楚。 您不能毫无歧义地从不同的位置导入2个FormsModule (例如,在导入中使用as foo ,然后使用foo.FormsModule引用它)。

这样,您可以使用来自任意第三方库的代码,并避免名称冲突。

角导入/导出用于使一个模块的内容可用于另一模块。

你的

导入:[FormsModule,AuthRoutingModule]

允许您使用从指令FormsModuleAuthRoutingModuleAuthModule并注册通过在这些模块提供的服务AppModule范围或闭合延迟加载根范围。

如果您在TypeScript代码中引用了任何Angulars指令或服务,则还需要添加TypeScript导入。 上面的FormsModuleAuthRoutingModule需要与TypeScript导入一起导入,以进行Angular imports: [...]

例如像

<form #f="ngForm">
  <input type="text">
</form>

仅当FormsModule列在当前模块的imports: [ ... ]中时有效。

由于没有TypeScript代码,因此不需要导入TypeScript。

是的,您可以通过在typescript类之前使用export关键字来正确使用,您可以在项目的其他地方使用该类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM