本文介绍了ModuleWithProviders 的 Angular 2 路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular 2 的新手,我需要关于路由部分的帮助.我正在使用 http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial

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

我有一个错误

导出的变量routing"具有或正在使用来自外部模块/home/frank/angular/node_modules/@angular/core/src/metadata/ng_module"的名称ModuleWithProviders",但无法命名.

这是我的代码

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 { }

对错误有什么想法吗?我还尝试了 seimport { ModuleWithProviders } from '@angular/core'; ting my "declaration": true, in tsconfig.js 并且还导入了

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

推荐答案

我通过以下方式解决了这个问题:

I solved this by doing:

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

不要忘记从@angular/core 导入 ModuleWithProviders

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

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

这篇关于ModuleWithProviders 的 Angular 2 路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:09