没有提供ActivatedRoute的提供者-Angular 2 RC5 [英] No provider for ActivatedRoute - Angular 2 RC5

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

问题描述

从RC4升级到Angular 2 RC5后,看来我无法再将ActivatedRoute注入到我的组件中.

After upgrading to Angular 2 RC5 (from RC4) it seems I can no longer inject ActivatedRoute into my components.

原始例外:没有提供ActivatedRoute的提供商!

ORIGINAL EXCEPTION: No provider for ActivatedRoute!

这是相关的代码段:

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

import { 
  ActivatedRoute
} from '@angular/router';

declare var module: {
  id: string;
};

@Component({
  moduleId: module.id,
  selector: 'mds-app',
  templateUrl: 'app.component.html',
  styleUrls: [
      'app.component.css'
  ],
  directives: []
})
export class AppComponent { 

  constructor(private _route: ActivatedRoute) {
    this._route.params.subscribe(params => console.log(_route));
  }
}

这是我的app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule }  from '@angular/common';
import { Routes, RouterModule }  from '@angular/router';

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

import { 
    routing, 
    appRoutingProviders 
} from './app.routing';

@NgModule({
  imports:      [ BrowserModule, CommonModule, RouterModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  providers:    [ appRoutingProviders ]
})
export class AppModule { }

我检查了英雄之旅"示例,他们做的事情完全相同,没有ActivatedRoute的提供程序声明,所以我想知道这里发生了什么?

I checked the "Tour of Heroes" example and they do the exact same thing, there is no provider declaration for ActivatedRoute so what is going on here I wonder?

推荐答案

我在GitHub上浏览了Angular 2问题,发现以上问题的解决方法完全是出于运气(请参阅

I was browsing through Angular 2 issues on GitHub and found the solution to the above problem out of sheer luck (see here).

我需要在NgModule中的imports中添加routing(请参阅上面的import)

I needed to add routing (see import above) to imports in NgModule, i.e.

@NgModule({
  imports:      [ BrowserModule, CommonModule, RouterModule, routing ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  providers:    [ appRoutingProviders ]
})

似乎Angular 2的错误消息比以前更加令人困惑.

Seems Angular 2's error messages just got more confusing than they already have been.

我希望这个答案对某人有用,我已经要拔头发了.

I hope this answer is of some use to someone, I was about to pull my hair out already.

根据普遍的要求,这是导入的routing的摘要(在我的头上,因为我本周下班了,请在评论中告知我是否有任何问题):

By popular request, here's a snippet for the imported routing (off the top of my head, as I'm off work this week, let me know in the comments if there are any issues with it):

app.routing.ts:

export routes: Routes = [
   { path: 'sales', component: SalesComponent }
];

export routing = RouterModule.forRoot(routes);

,然后在您的app.module.ts中,将其导入如下:

and in your app.module.ts you'd import this as follows:

import { routing } from 'app.routing'

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

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