无法在routerOnActivate上获取查询参数

无法在routerOnActivate上获取查询参数

本文介绍了无法在routerOnActivate上获取查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用新的Route库获取查询参数时遇到问题.

Im having problems trying to get query params using the new Route library.

版本2.0.0-rc.1

VERSION2.0.0-rc.1

问题

routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
  console.log(curr.getParam("test"));
}

始终打印未定义

另一个问题是我的网址每次都会重置"(这就是为什么我认为curr.getParam("test")返回未定义的原因)

这是我的App组件和Home组件:

Here is my App Component and My Home Component :

APP组件

import {Component} from '@angular/core';
import { Routes, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router } from '@angular/router'
import { HomeComponent } from './home.component';

@Component({
    selector    : 'my-app',
    template : '<router-outlet></router-outlet>',
    directives  : [ ROUTER_DIRECTIVES ],
    providers   : [ ROUTER_PROVIDERS ]
})
@Routes([
    {
        path : '/',
        component : HomeComponent
    }
])
export class AppComponent {
    constructor (private _router : Router) {
    }
}

家庭组件

import { Component } from '@angular/core';
import { Router, OnActivate, RouteSegment } from '@angular/router';


@Component({
    selector: 'home',
    template: '<h3>THIS IS HOME</h3>'
})
export class HomeComponent implements OnActivate{
    constructor(private _router : Router) {
        console.log('Called');
    }

    routerOnActivate(curr:RouteSegment):void {
        console.log(curr.getParam("test"));
    }
}

测试网址

localhost:3000/?test = helloworld->更改为(问题)localhost:3000

localhost:3000/?test=helloworld --> Changes to(problem) localhost:3000

有人可以帮忙吗?

推荐答案

您的代码看起来不错,唯一的问题是根据官方Angular 2文档:

Your code looks good, the only problem is that as per official Angular 2 documentation:


因此,您可以通过从


So you can get the value of optional parameter (test) from current RouteSegment by change your address from

localhost:3000/?test = helloworld

localhost:3000/?test=helloworld

localhost:3000/; test = helloworld

localhost:3000/;test=helloworld

这篇关于无法在routerOnActivate上获取查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:53