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

问题描述

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

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

查看此。如何在不收到此警告的情况下使用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 },
])

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

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.

请参见为例。 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”的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:37