Angular 2 RC5:没有路由器的提供商 [英] Angular 2 RC5 : No provider for Router

查看:95
本文介绍了Angular 2 RC5:没有路由器的提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的Angular 2 RC5路由器遇到问题(路由器版本是RC1)。

I am facing an issue using the new Angular 2 RC5 router (router version is RC1).

这是我从开发控制台获得的日志:

Here is the log I get from the dev console:

EXCEPTION: Error in /templates/app.component.html:2:0
ORIGINAL EXCEPTION: No provider for Router!

这是我的app.modules.ts的样子:

Here is what my app.modules.ts looks like:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent }   from './components/app.component';
import { routing } from './app.routes';

@NgModule({
    declarations: [AppComponent],
    imports:      [BrowserModule, RouterModule, FormsModule, HttpModule, routing],
    bootstrap:    [AppComponent],
})
export class AppModule {}

这是我的boot.ts文件:

Here is my boot.ts file:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

我的app.routes.ts文件......

My app.routes.ts file...

import { Routes, RouterModule } from '@angular/router';

// Components
import { HomeComponent } from './components/home.component';

const routes: Routes = [
    // Root
    { path: '/', name: 'Home', component: HomeComponent, useAsDefault: true},
];

// - Updated Export
export const routing = RouterModule.forRoot(routes); 

...和我的app.component.ts文件:

...and my app.component.ts file:

import { Component, ViewContainerRef } from '@angular/core';

@Component({
    selector: 'app',
    templateUrl: '/templates/app.component.html'
})

export class AppComponent {
    viewContainerRef: any;

    public constructor(viewContainerRef:ViewContainerRef) {
        // You need this small hack in order to catch application root view container ref
        this.viewContainerRef = viewContainerRef;
    }
}

我在这里缺少什么?

推荐答案

在app.routes.ts文件中:

Inside app.routes.ts file:

添加此导入状态

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

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

这应该有效。

这篇关于Angular 2 RC5:没有路由器的提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆