Angular 2获取路线没有Route的提供者 [英] Angular 2 get route no provider for Route

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

问题描述

在Angular 2中,我希望能够使用route.path来获取路径.

In Angular 2 I want to be able to use route.path to.. get the path.

在组件构造函数内部,我拥有:

Inside a components constructor I have:

constructor(private route: Route) {}

所以我想打电话给

this.route.path

对于应用程序引导,我得到了:

And for the app bootstrap I got:

bootstrap(AppComponent,
[
    ROUTER_PROVIDERS,
]);

但是我仍然收到没有提供路线的服务!错误

推荐答案

我假设您要注入Location而不是Route. Route用于路由配置,Angular无法知道要注入哪个Route实例.

I assume what you to inject Location instead of Route. Route is for route configuration and there is no way for Angular to know which Route instance to inject.

Location提供path()函数以获取当前URL.

Location provides the path() function to get the current URL.

根据Angular2版本,有不同的导入路径.

Depending on the Angular2 version there are different paths where to import it from.

另请参见 Location和HashLocationStrategy在beta.16中停止工作

< = beta 15

import {Location} from 'angular2/router';

> = beta 16< rc.0

import {Location} from 'angular2/platform/common';  

constructor(private location:Location) {
  console.log(location.path());
}

> = rc.0

import {Location} from '@angular/common';

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

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