加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

routing – Angular 2错误:无法解析’RouteParams’的所有参数

发布时间:2020-12-17 08:32:31 所属栏目:安全 来源:网络整理
导读:尝试使用RouteParams来获取查询字符串参数,但我只是得到了错误 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 decorate
尝试使用RouteParams来获取查询字符串参数,但我只是得到了错误

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?

重要文件是:

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建议的那样,我添加了以下路由配置:

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

但我仍然得到同样的错误(Plnkr更新)…

从中删除RouteParams
bootstrap(AppComponent,RouteParams]);

RouteParams由路由器提供.如果你自己提供它注入失败.

有关示例,请参见https://angular.io/api/router/Params. RouteParams只能注入路由器添加的组件.

Plunker example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读