无法注入ActivatedRouteSnapshot [英] Unable to inject ActivatedRouteSnapshot

查看:149
本文介绍了无法注入ActivatedRouteSnapshot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将ActivatedRouteSnapshot注入组件不起作用(也没有注入ActivatedRoute).这是堆栈跟踪:

Injecting ActivatedRouteSnapshot into a component is not working (and neither is injecting ActivatedRoute). Here the stack trace:

"Error: Can't resolve all parameters for ActivatedRouteSnapshot: (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?).
at SyntaxError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:7042:33)
at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:73735:16)
at new SyntaxError (http://localhost:4200/vendor.bundle.js:6140:16)
at CompileMetadataResolver._getDependenciesMetadata (http://localhost:4200/vendor.bundle.js:19345:31)
at CompileMetadataResolver._getTypeMetadata (http://localhost:4200/vendor.bundle.js:19220:26)
at CompileMetadataResolver._getInjectableMetadata (http://localhost:4200/vendor.bundle.js:19208:21)
at CompileMetadataResolver.getProviderMetadata (http://localhost:4200/vendor.bundle.js:19450:40)
at http://localhost:4200/vendor.bundle.js:19408:49
at Array.forEach (native)
at CompileMetadataResolver._getProvidersMetadata (http://localhost:4200/vendor.bundle.js:19375:19)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://localhost:4200/vendor.bundle.js:18848:30)
at CompileMetadataResolver._loadDirectiveMetadata (http://localhost:4200/vendor.bundle.js:18736:23)
at http://localhost:4200/vendor.bundle.js:18937:54
at Array.forEach (native)
at CompileMetadataResolver.loadNgModuleDirectiveAndPipeMetadata (http://localhost:4200/vendor.bundle.js:18936:41)"


在该组件中,ActivatedRouteSnapshot如下注入:


In the component, ActivatedRouteSnapshot is injected as follows:

constructor([...], private router: Router,
          private route: ActivatedRouteSnapshot) {
    [...]
}


app.component.ts中提供了

ActivatedRouteSnapshot:


ActivatedRouteSnapshot is provided in app.component.ts:

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
    providers: [[...], ActivatedRouteSnapshot]
})


我正在尝试类似于在此处访问查询参数的方式: https://stackoverflow.com/a/38997116/3433306

根据文档,它应该像将其添加到构造函数参数一样简单:

According to the docs it should be as simple as adding it to the constructor parameters: https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html

要成功注入ActivatedRouteSnapshot,我缺少什么?

What am I missing to successfully inject ActivatedRouteSnapshot?

推荐答案

使用 ActivatedRoute 而不是ActivatedRouteSnapshot.然后,您可以像这样使用快照:

Use ActivatedRoute instead of ActivatedRouteSnapshot. Then you can use the snapshot like this:

constructor(activatedRoute: ActivatedRoute) { 
  var snapshot = activatedRoute.snapshot;
}

这篇关于无法注入ActivatedRouteSnapshot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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