![](/img/trans.png)
[英]Is it possible for angular2 to import components from another angular2 project?
[英]Angular2 import components/services from module
我正在研究Angular2最終應用程序(當前),該應用程序有兩個模塊:
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 {}
核心模塊:
/**
* 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'。
任何想法為什么這不起作用? 我的目標是在模塊中定義某些組件/服務,以在我將創建的其他模塊中重用。 需要找出正確的方法來執行此操作...
感謝您的任何幫助!
服務不需要添加到NgModule的exports
中。
在CoreModule中provide
它們,或者像現在在AppModule中provide
它們,但是從它們的原始File中導入它們...
或export {AuthService} from '..File..'
向您的CoreModule添加export {AuthService} from '..File..'
。
您只能import
從那里他們是一個文件組件和服務export
版。 而且不是它們在模塊的exports
部分中的位置。這是打字稿而不是Angular! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.