Angular 2错误:无法解析“ RouteParams”的所有参数 [英] Angular 2 error: Cannot resolve all parameters for 'RouteParams'

查看:124
本文介绍了Angular 2错误:无法解析“ RouteParams”的所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用RouteParams获取查询字符串参数,但我只是遇到错误

Trying to use RouteParams to get query-string parameters, but I just get the error


无法解析 RouteParams的所有参数(?)。确保所有
参数都用Inject修饰或具有有效的
类型注解,并且 RouteParams用Injectable修饰。
angular2-polyfills.js:332错误:无法读取
的属性 getOptional未定义...。

Cannot resolve all parameters for 'RouteParams'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RouteParams' is decorated with Injectable. angular2-polyfills.js:332 Error: Cannot read property 'getOptional' of undefined.......

查看此 Plnkr 。如何在不收到此警告的情况下使用RouteParams?

Check out this Plnkr. How can I use RouteParams without getting this warning?

重要文件为:

boot.ts

import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, RouteParams} from 'angular2/router';
import {AppComponent} from './app/app.component';

bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);

app.component.ts

import {Component, OnInit} from 'angular2/core';
import {RouteParams} from "angular2/router";

@Component({
  selector: 'app',
  template: `
    <p *ngIf="guid">guid gived is: {{guid}}</p>
    <p *ngIf="!guid">No guid given in query-string in URL</p>
  `
})
export class AppComponent implements OnInit {
  guid:string;

  constructor(private _params: RouteParams) {} //

  ngOnInit() {
    this.guid = this._params.get('guid');
  }

}

更新:

我没有任何路由,我只有默认的根路由(如果在我没有其他路由的情况下可以将其称为所有路由... )。但正如Gianluca所建议的那样,我添加了以下路由配置:

I am not having any routes, I just have the default root route (if that can be called a route at all when I have no other routes...). But as suggested by Gianluca, I added the following route config:

@RouteConfig([
  { path: '/:guid', name: 'Home', component: AppComponent, useAsDefault: true },
])

但是我仍然遇到相同的错误( Plnkr 已更新)。 。

But I still get the same error (the Plnkr is updated)...

推荐答案


删除 RouteParams

bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);

RouteParams 由路由器提供。如果您自己提供,则注入失败。

RouteParams is provided by the router. If you provide it yourself injecting it fails.

请参见 https://angular.io/api/router/Params 为例。 RouteParams 只能注入到路由器添加的组件上。

See https://angular.io/api/router/Params for an example. RouteParams can only be injected on components added by the router.

柱塞示例

这篇关于Angular 2错误:无法解析“ RouteParams”的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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