我正在开发一个Angular2最终应用程序,它目前有两个模块:
-
CoreModule:包含共享组件和服务 .
-
AppModule:应用程序的根模块
的AppModule:
/**
* Created by jamdahl on 9/21/16.
*/
// Angular Imports
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpModule} from '@angular/http';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {CoreModule} from '../core-module/core.module';
import {UserService, AuthService, AuthComponent} from '../core-module/core.module';
// Components
import {HomePageComponent} from './components/home-page.component';
//import {enableProdMode} from '@angular/core';
//enableProdMode();
@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
ReactiveFormsModule,
CoreModule
],
declarations: [
AuthComponent,
HomePageComponent
],
providers: [
AuthService,
UserService
],
bootstrap: [
HomePageComponent
]
})
export class AppModule {}
CoreModule:
/**
* Created by jamdahl on 9/21/16.
*/
// Angular imports
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpModule} from '@angular/http';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
// Class imports
import {User} from './classes/user.class';
import {Alert} from './classes/alert.class';
// Service imports
import {AuthService} from './services/auth.service';
import {UserService} from './services/user.service';
// Component imports
import {AuthComponent} from './components/auth.component';
import {SignInComponent} from './components/signin.component';
import {SignUpComponent} from './components/signup.component';
//import {enableProdMode} from '@angular/core';
//enableProdMode();
@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
ReactiveFormsModule
],
declarations: [
AuthComponent,
SignInComponent,
SignUpComponent
],
providers: [],
exports: [
User,
Alert,
AuthService,
UserService,
AuthComponent
]
})
export class CoreModule {}
当我尝试运行它时,我得到以下内容:
错误./src/view/app-module/app.module.ts(11,9):错误TS2305:模块'“/ Users / jamdahl / Web / Web-Scratch / Angular2-Express-Mongoose / src / view /core-module/core.module“'没有导出的成员'UserService' . 错误在./src/view/app-module/app.module.ts(11,22):错误TS2305:模块'“/ Users / jamdahl / Web / Web-Scratch / Angular2-Express-Mongoose / src / view / core-module / core.module“'没有导出成员'AuthService' . 错误在./src/view/app-module/app.module.ts(11,35):错误TS2305:模块'“/ Users / jamdahl / Web / Web-Scratch / Angular2-Express-Mongoose / src / view / core-module / core.module“'没有导出的成员'AuthComponent' .
任何想法为什么这不起作用?我的目标是在模块中定义某些组件/服务,以便在我将创建的其他模块中重用 . 需要找出正确的方法来做到这一点......
感谢您的任何帮助!
1 回答
服务不需要添加到NgModule的
exports
.provide
它们位于您的CoreModule内部,或者现在位于AppModule内部,但是从原始文件导入它们...或者将
export {AuthService} from '..File..'
添加到您的CoreModule .您只能
import
来自文件的import
组件和服务export
ed . 而不是它们在模块的exports
部分中的位置..这是一个打字稿的东西,而不是一个Angular的东西! :)