gpt4 book ai didi

typescript - 获取路由查询参数

转载 作者:太空狗 更新时间:2023-10-29 17:00:28 27 4
gpt4 key购买 nike

我正在尝试从 rc1 迁移到 rc4,但在获取查询字符串参数时遇到了问题。 ActivatedRoute 对象始终为空。

hero.component.ts

import {Component, OnInit} from "@angular/core";
import {Control} from "@angular/common";
import {ROUTER_DIRECTIVES, ActivatedRoute} from '@angular/router';

@Component({
template: '../partials/main.html',
directives: [ROUTER_DIRECTIVES]
})

export class HeroComponent implements OnInit {

constructor(private _activatedRoute: activatedRoute) {

}

ngOnInit() {
this._activatedRoute.params.subscribe(params => {
console.log(params);
});
}
}

ma​​in.ts

import {bootstrap} from '@angular/platform-browser-dynamic';
import {HTTP_PROVIDERS, RequestOptions, Http} from '@angular/http';
import {AppRouterProviders} from './app.routes';

bootstrap(AppComponent, [
AppRouterProviders,
HTTP_PROVIDERS
]);

app.component.ts

import {Component, OnInit} from '@angular/core';
import {HeroComponent} from './hero.component';
import {RouteConfig, Router, ROUTER_DIRECTIVES} from '@angular/router';


@Component({
selector: 'my-app',
templateUrl: '../partials/main.html',
directives: [
HeroComponent,
ROUTER_DIRECTIVES
]
})

export class AppComponent {
}

partials/main.html

<a class="nav-link" [routerLink]="['/']" [queryParams]="{st: 'new'}">New</a>

app.routes.ts

import {provideRouter, RouterConfig}  from '@angular/router';
import {HeroComponent} from './hero.component';
import {ErrorComponent} from './error.component';

const routes: RouterConfig = [
{path:'', component: HeroComponent},
{path:'**', component: ErrorComponent}
];

export const AppRouterProviders = [
provideRouter(routes)
];

当我点击“新建”链接时,控制台打印出空对象

Object {}

已更新

plunker

最佳答案

更新(2.0.0 最终版)

(somepath/:someparam/someotherpath) 你可以使用 _router.queryParams.subscribe(...) 订阅它们:

export class HeroComponent implements OnInit {
constructor(private _activatedRoute: ActivatedRoute, private _router:Router) {
_activatedRoute.queryParams.subscribe(
params => console.log('queryParams', params['st']));

原创

如果你想要 queryParams 而不是路由参数 (somepath/:someparam/someotherpath) 你可以使用 _router.routerState.queryParams.subscribe( ...):

export class HeroComponent implements OnInit {
constructor(private _activatedRoute: ActivatedRoute, private _router:Router) {
_router.routerState.queryParams.subscribe(
params => console.log('queryParams', params['st']));

Plunker example

关于typescript - 获取路由查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38488568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com