简体   繁体   English

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

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

I am pretty new in Angular 2 . 我在Angular 2还很陌生 I am studying how to create modules into an Angular app and I have the following doubt related a tutorial that I am following. 我正在研究如何在Angular应用程序中创建模块,并且我对以下与我正在关注的教程有关的疑问有所疑问。

My doubt is related to the routing. 我的怀疑与路由有关。

So in my example there is defined this AuthModule module: 因此,在我的示例中定义了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 {}

and I have the related rotues configuration class defined: 并且我定义了相关的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 {

}

So I think that the export keyword means that the content related to this class can be exported and used somewhere else (in this case I think into the imports array of the AuthModule class). 因此,我认为export关键字意味着可以将该类相关的内容导出并在其他地方使用(在这种情况下,我认为是AuthModule类的imports数组)。

Is it? 是吗? Or am I missing something? 还是我错过了什么? What it the exact meaning of the export statment? 出口声明的确切含义是什么?

I am not understanding if it is something related to Angular or more generally to TypeScript (because here I found https://www.typescriptlang.org/docs/handbook/modules.html ). 我不了解它是否与Angular有关,或更笼统地说与TypeScript有关(因为在这里我找到了https://www.typescriptlang.org/docs/handbook/modules.html )。 So it seems to me that this module concept is not directly bounded to Angular 2 framework but is a TypeScript concept to subdivide our code in a smart way (then Angular 2 can use this kind of feature of the language). 因此在我看来,这个模块概念并不直接绑定到Angular 2框架,而是一个TypeScript概念,可以用一种灵巧的方式细分我们的代码(然后Angular 2可以使用这种语言的功能)。

Is it or am I missing something? 是我还是缺少什​​么?

Angular imports/exports and TypeScript imports/exports are two different concepts. Angular导入/导出和TypeScript导入/导出是两个不同的概念。

TypeScript imports/exports work at language level to make it clear what a used identifier references exactly. TypeScript导入/导出在语言级别进行工作,以使所使用的标识符准确地引用什么。 This is entirely unrelated to Angular. 这与Angular完全无关。

So, if you use FormsModule there can't be any ambiguity, what FormsModule is meant. 因此,如果使用FormsModule ,就不会有任何歧义,这就是FormsModule的意思。 If there is more than one FormsModule in your code or any of your dependencies, then you need to make it clear with imports which one is meant. 如果您的代码或任何依赖FormsModule中有多个FormsModule ,则需要通过导入使其含义清楚。 You can't import 2 FormsModule from different locations without disambiguation (for example using as foo in the import and then reference it using foo.FormsModule ). 您不能毫无歧义地从不同的位置导入2个FormsModule (例如,在导入中使用as foo ,然后使用foo.FormsModule引用它)。

This way you can use code from arbitrary 3rd-party libraries and avoid name collisions. 这样,您可以使用来自任意第三方库的代码,并避免名称冲突。

Angular imports/exports are used to make the content of one module available to be used in another module. 角导入/导出用于使一个模块的内容可用于另一模块。

Your 你的

imports: [ FormsModule, AuthRoutingModule ] 导入:[FormsModule,AuthRoutingModule]

Allows you to use the directives from FormsModule and AuthRoutingModule in AuthModule and registers the services provided by these modules in the AppModule scope or the closed lazy-loaded root scope. 允许您使用从指令FormsModuleAuthRoutingModuleAuthModule并注册通过在这些模块提供的服务AppModule范围或闭合延迟加载根范围。

If you reference any of Angulars directives or services in TypeScript code, you also need to add TypeScript imports. 如果您在TypeScript代码中引用了任何Angulars指令或服务,则还需要添加TypeScript导入。 Above FormsModule and AuthRoutingModule need to be imported with TypeScript imports, to make the Angular imports: [...] work. 上面的FormsModuleAuthRoutingModule需要与TypeScript导入一起导入,以进行Angular imports: [...]

For example like 例如像

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

works only if FormsModule is listed in imports: [ ... ] of your current module. 仅当FormsModule列在当前模块的imports: [ ... ]中时有效。

There is no TypeScript import required, because there is no TypeScript code. 由于没有TypeScript代码,因此不需要导入TypeScript。

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

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

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