Angular 2获取路线没有Route的提供者 [英] Angular 2 get route no provider for 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屋!