Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何导出不是Angular 4模块中的组件、指令或管道的类/模型?_Javascript_Angular_Typescript_Es6 Modules - Fatal编程技术网

Javascript 如何导出不是Angular 4模块中的组件、指令或管道的类/模型?

Javascript 如何导出不是Angular 4模块中的组件、指令或管道的类/模型?,javascript,angular,typescript,es6-modules,Javascript,Angular,Typescript,Es6 Modules,我了解如何创建一个包含组件、指令、管道的模块,然后将它们导出到另一个模块中。它很好用 但是假设我有一个通用模块“GenericCarList”,它有一个组件 它可以接收信息,用有用的信息构建一个漂亮的汽车列表,因此,我可以将我的模块定义为: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { GenericCarListComponent } from

我了解如何创建一个包含组件、指令、管道的模块,然后将它们导出到另一个模块中。它很好用

但是假设我有一个通用模块“GenericCarList”,它有一个组件 它可以接收信息,用有用的信息构建一个漂亮的汽车列表,因此,我可以将我的模块定义为:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GenericCarListComponent } from './genericCarList.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    GenericCarListComponent 
  ],
  exports: [
    GenericCarListComponent
]
})
export class GenericCarListModule{ }
所以,我在上面声明GenericCarListModule并导出GenericCarListComponent,以供使用GenericCarListModule的其他模块使用

在另一个有AppModule的项目中,我可以导入GenericCarListModule并使用GenericCarListComponent,只需在html文件中添加以下标记:

<app-genericCarListComponent>My generic Car List</app-genericCarListComponent>
它很好用!:)

但是我希望在我的GenericCarListModule中有有用的类和模型。例如:

//a car model
//car.ts

export class car {
    maxSpeed: number;
    color: string;

}

如果它们是组件、管道或指令,我可以像以前一样导出它们:

 exports: [
        GenericCarListComponent
    ]
在英语中,他们说:

我应该出口什么

导出其他NgModules中的组件可以声明的类 在其模板中引用。这些是你们的公开课。如果你 不要导出类,它是私有的,仅对其他类可见 在此模块中声明的组件

您可以导出任何可声明的类组件、指令和 管道,无论是在此模块中声明还是在导入的 NgModule


由于它们不是可声明的类组件、指令和管道,在我的AppModule中导出然后导入这些类的正确方法是什么?

为此使用真实的模块
NgModule
s首先是一件很糟糕的事情,很高兴你不必在所有事情上都使用它们。你不需要在app module中声明这些东西,你只需要用import语句导入它们,然后根据需要在其他组件/指令/管道/服务中使用它们。模块导入/声明仅适用于需要在模板中解析并向注入器注册的内容,因此组件、指令和管道、提供程序适用于需要向注入器注册的内容,所以服务。@AluanHaddad你的意思是我应该有一个带有GenericCarListComponent的NgModule和一个带有我的汽车模型和carCommons类的普通typescript模块?是吗?是的。同样,ES模块对于React、Aurelia、Vue和Ember等框架来说已经足够好了,但Angular决定与众不同,并用自己的非正交不透明模块概念(在Angular 2.0-beta-rc.5中引入…)来迷惑每个人。现在您必须使用这两种方法来学习它们的框架。但是ES模块(您称之为TS模块)是语言的基础,比框架更深。我喜欢文档中的措辞:“导出可声明类”`
//a car commons class
    //carCommons.ts

export class carCommons {

    //some useful methods that is useful for anyone that is using this list car module.

}
 exports: [
        GenericCarListComponent
    ]