stu*_*rsi 39 typescript angular2-routing angular
将ActivatedRouteSnapshot注入组件不起作用(也没有注入ActivatedRoute).这里的堆栈跟踪:
"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)"
Run Code Online (Sandbox Code Playgroud)
在组件中,ActivatedRouteSnapshot注入如下:
constructor([...], private router: Router,
private route: ActivatedRouteSnapshot) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
ActivatedRouteSnapshot提供于app.component.ts:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [[...], ActivatedRouteSnapshot]
})
Run Code Online (Sandbox Code Playgroud)
我试图访问查询参数类似于如何在这里完成:https://stackoverflow.com/a/38997116/3433306
根据文档,它应该像添加到构造函数参数一样简单:https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html
成功注入ActivatedRouteSnapshot我缺少什么?
Mar*_*ers 103
使用ActivatedRoute而不是ActivatedRouteSnapshot.然后你可以像这样使用快照:
constructor(activatedRoute: ActivatedRoute) {
var snapshot = activatedRoute.snapshot;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24873 次 |
| 最近记录: |