Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Angular 仅在角2中导入模块一次_Angular_Angular2 Forms_Angular2 Services - Fatal编程技术网

Angular 仅在角2中导入模块一次

Angular 仅在角2中导入模块一次,angular,angular2-forms,angular2-services,Angular,Angular2 Forms,Angular2 Services,对angular 2来说是个新手,还在学习。 据我所知,在Angular 2中使用模块并在应用程序中导入模块是因为它们与孩子共享。在下面的代码中,我试图在应用程序范围内共享ReactiveFormsModule,但应用程序在编译时崩溃。 我确实使用forRoot方法为我的自定义模块实现了这一点,但我不明白如何使用内置模块实现这一点 app.module.ts import { NgModule } from '@angular/core'; import { HttpModule } from

对angular 2来说是个新手,还在学习。 据我所知,在Angular 2中使用模块并在应用程序中导入模块是因为它们与孩子共享。在下面的代码中,我试图在应用程序范围内共享ReactiveFormsModule,但应用程序在编译时崩溃。 我确实使用forRoot方法为我的自定义模块实现了这一点,但我不明白如何使用内置模块实现这一点

app.module.ts

import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';
import { AppRoutingModule } from "./app-routing.module"
import { AppComponent } from './app.component';

import { HomeModule } from "./home/home.module";
import { AuthenticationModule } from "./authentication/authentication.module";
import { APIServiceModule } from './services/api-service.module';


@NgModule({
  imports: [HttpModule,
    ReactiveFormsModule,
    AppRoutingModule,
    AuthenticationModule,
    HomeModule,
    APIServiceModule.forRoot()
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }
import { NgModule } from "@angular/core";
import { BrowserModule } from '@angular/platform-browser';

import { AuthenticationComponent } from "./authentication.component";
import { AuthenticationRoutingModule } from "./authentication-routing.module"
@NgModule({
    imports: [AuthenticationRoutingModule, BrowserModule],
    declarations: [AuthenticationComponent]
})
export class AuthenticationModule { }
authentication.module.ts

import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';
import { AppRoutingModule } from "./app-routing.module"
import { AppComponent } from './app.component';

import { HomeModule } from "./home/home.module";
import { AuthenticationModule } from "./authentication/authentication.module";
import { APIServiceModule } from './services/api-service.module';


@NgModule({
  imports: [HttpModule,
    ReactiveFormsModule,
    AppRoutingModule,
    AuthenticationModule,
    HomeModule,
    APIServiceModule.forRoot()
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }
import { NgModule } from "@angular/core";
import { BrowserModule } from '@angular/platform-browser';

import { AuthenticationComponent } from "./authentication.component";
import { AuthenticationRoutingModule } from "./authentication-routing.module"
@NgModule({
    imports: [AuthenticationRoutingModule, BrowserModule],
    declarations: [AuthenticationComponent]
})
export class AuthenticationModule { }

AuthenticationModule
AppModule
中导入,我希望在模块之间共享被动表单模块。但编译时失败。

您可以将其导入一个公共模块,如:

@NgModule({
    imports: [ReactiveFormsModule],
exports:[ReactiveFormsModule]
})
export class AppCommonsModule{}
您也可以在此处为所有子模块提供通用的应用程序级服务,但如果您希望为您的应用程序提供单例服务,请小心。不要在公共模块中提供任何单例服务。
您只需要在所有子模块中导入一个模块

您实际上可以导出模块和组件,尽管大多数联机示例都是关于导出组件的。