angular 获取url中是否有orderId的查询参数以及它的值
可以使用 ActivatedRoute 来获取当前路由中的查询参数,然后通过 paramMap 对象获取指定参数的值。
以下是一个获取 orderId 查询参数的示例代码:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {
orderId: string;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.orderId = params.get('orderId');
});
}
}
在 ngOnInit 生命周期钩子函数中,使用 ActivatedRoute 的 paramMap 属性订阅路由参数的变化。在回调函数中,使用 get 方法获取指定参数的值。如果查询参数中不存在 orderId,this.orderId 的值将为 null
原文地址: https://www.cveoy.top/t/topic/dCNy 著作权归作者所有。请勿转载和采集!