Angular 2 Routing Error with ModuleWithProviders

怎甘沉沦 提交于 2019-12-22 10:36:39

问题


I'm new in Angular 2 I need help on the routing part. I'm using http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial

I got an error

Exported variable 'routing' has or is using name 'ModuleWithProviders' from external module "/home/frank/angular/node_modules/@angular/core/src/metadata/ng_module" but cannot be named.

Here is my code

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { HttpModule } from '@angular/http';

// used to create fake backend
import { fakeBackendProvider } from './_helpers/index';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { BaseRequestOptions } from '@angular/http';

import { AppComponent }  from './app.component';
import { routing }        from './app.routing';

import { AlertComponent } from './_directives/index';
import { AuthGuard } from './_guards/index';
import { AlertService, AuthenticationService, UserService } from './_services/index';
import { HomeComponent } from './home/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';

@NgModule({
    imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing
 ],
declarations: [
AppComponent,
AlertComponent,
HomeComponent,
LoginComponent,
RegisterComponent
],
providers: [
    AuthGuard,
    AlertService,
    AuthenticationService,
    UserService,

    // providers used to create fake backend
    fakeBackendProvider,
    MockBackend,
    BaseRequestOptions
],
bootstrap: [AppComponent]
})

 export class AppModule { }

Any ideas on the error? I also tried seimport { ModuleWithProviders } from '@angular/core';tting my "declaration": true, in tsconfig.js and also imported


回答1:


I solved this by doing:

export const routing: ModuleWithProviders = RouterModule.forRoot(APP_ROUTES);

And don't forgot to import ModuleWithProviders from @angular/core

import { ModuleWithProviders } from "@angular/core";



回答2:


You should not need to import ModuleWithProviders. I think that you only needed to import that module in older Release Candiades (RCs). Currently, importing MgModule will automatically allow providers which seems to be the only purpose for ModuleWithProviders DOCS



来源:https://stackoverflow.com/questions/41134288/angular-2-routing-error-with-modulewithproviders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!