来自模块

时间:2016-09-21 16:00:50

标签: angular typescript web-frontend

我正在开发一个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 个答案:

答案 0 :(得分:9)

不需要将服务添加到NgModule的exports

provide它们位于您的CoreModule内部,或者现在位于AppModule内部,但是从原始文件中导入它们......

或者在您的CoreModule中添加export {AuthService} from '..File..'

您只能import来自export个文件的exports组件和服务。而不是它们在模块的SELECT h.[Priority] ,h.[FPNNumber] ,h.[ProjectName] ,sp.SponsorName ,pm.ProjectManagerName ,o.[OrgName] ,HealthID ,[StrategicAlignmentID] + [FinancialAlignmentID] + [TechnologyAlignmentID] As ProjectScore ,h.[Cost] ,h.[Budget] ,h.[PercentComplete] ,Convert(varchar(10),h.[EstimatedImpDt], 101) As EstimatedImpDt ,[EstimatedROI] ,h.[ExpectedBenefit] ,CONVERT(Decimal(10, 2),((DATEDIFF(day, h.[PaybackPerioddt], GETDATE()))/365.0)) As PaybackPerioddt FROM [ProjectNew].[Header] h join ProjectNew.Sponsor sp ON h.SponsorID = sp.SponsorID join ProjectNew.ProjectManager pm ON h.ProjectManagerID = pm.ProjectManagerID Join ProjectNew.Organization o ON h.OrgID = o.OrgID where StatusID is null or StatusID < 12 部分中的位置..它是一个打字稿的东西,而不是一个Angular的东西! :)