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

And*_*ili 2 javascript javascript-framework typescript angular

我在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 {}
Run Code Online (Sandbox Code Playgroud)

并且我定义了相关的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 {

}
Run Code Online (Sandbox Code Playgroud)

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

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

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

是我还是缺少什​​么?

Gün*_*uer 5

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

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

因此,如果您使用FormsModule不存在任何歧义,那FormsModule意味着什么。如果FormsModule您的代码或任何依赖项中有一个以上的代码,则需要通过导入清楚地表明含义是什么。您不能FormsModule毫无歧义地从不同的位置导入2 (例如as foo,在导入中使用,然后使用进行引用foo.FormsModule)。

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

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

你的:

imports: [
    FormsModule,
    AuthRoutingModule
  ]
Run Code Online (Sandbox Code Playgroud)

允许您在范围内或在延迟加载的根范围内使用这些模块中的指令FormsModuleAuthRoutingModuleAuthModule其中注册这些模块提供的服务AppModule

如果您在TypeScript代码中引用了任何Angulars指令或服务,则还需要添加TypeScript导入。在上方FormsModuleAuthRoutingModule需要使用TypeScript导入来导入,以使Angular imports: [...]正常工作。

例如像

<form #f="ngForm">
  <input type="text">
</form>
Run Code Online (Sandbox Code Playgroud)

FormsModuleimports: [ ... ]当前模块中列出时有效。

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