如果要使用查询参数而不在路由中定义它们,则可以在构造函数中引用ActivatedRouting并订阅queryParams。
constructor(private route: ActivatedRoute) {
this.route
.queryParams
.subscribe(params => {
if (params) {
console.log(params);
}
});
}
比如请求的为:
http://localhost:4200/?89789=78&tyrt=t
那么打印的为:
{89789: "78", tyrt: "t"}
比如:一定加http://尾号
const url = 'http://127.0.0.1:4200?';
const p = 'SerialNumber=' + data.SerialNumber +'&Name='+data.Name+'&Type='+data.Type
+'&Year='+data.TestDate.Year+'&Month='+data.TestDate.Month+'&Day='+data.TestDate.Day
+'&Hour='+data.TestDate.Hour+'&Minute='+data.TestDate.Minute+'&Second='+data.TestDate.Second;
window.open(encodeURI(url+p));
参数解析,能够解析127.0.0.1:4200/indexxxxxxx.html?para='xxxx'和127.0.0.1:4200/index/heh/ttt/SN/1200/xxx等后10个确定的路径。这是两种路径,下面是解析代码:
try {
if (this.route.queryParams['value'] && this.route.queryParams['value']['para']) {//解析带参数的
const qp: string = this.route.queryParams['value']['para'];
const qps = qp.split('|');
if (qps.length >= 10) {
const searchTypeName = qps[0];
if (searchTypeName === 'SN') {
this.searchType = 0;
} else if (searchTypeName === 'TID') {
this.searchType = 1;
} else if (searchTypeName === 'SN') {
this.searchType = 2;
}
this.searchParam = qps[1];
this.resultType = Number(qps[2]);
this.resultName = qps[3];
const year = Number(qps[4]);
const month = Number(qps[5]);
const day = Number(qps[6]);
const hour = Number(qps[7]);
const minute = Number(qps[8]);
const second = Number(qps[9]);
this.time = new rserver.DateAndTime({
'Year': year,
'Month': month,
'Day': day,
'Hour': hour,
'Minute': minute,
'Second': second
});
}
} else {
if (this.route.url['value'] && this.route.url['value']['length'] >= 10) {//解析不带参数的
const routePathLength = +this.route.url['value']['length'];
const searchTypeName = this.route.url['value'][routePathLength - 10]['path'];
if (searchTypeName === 'SN') {
this.searchType = 0;
} else if (searchTypeName === 'TID') {
this.searchType = 1;
} else if (searchTypeName === 'SN') {
this.searchType = 2;
}
this.searchParam = this.route.url['value'][routePathLength - 9]['path'];
this.resultType = Number(this.route.url['value'][routePathLength - 8]['path']);
this.resultName = this.route.url['value'][routePathLength - 7]['path'];
const year = Number(this.route.url['value'][routePathLength - 6]['path']);
const month = Number(this.route.url['value'][routePathLength - 5]['path']);
const day = Number(this.route.url['value'][routePathLength - 4]['path']);
const hour = Number(this.route.url['value'][routePathLength - 3]['path']);
const minute = Number(this.route.url['value'][routePathLength - 2]['path']);
const second = Number(this.route.url['value'][routePathLength - 1]['path']);
this.time = new rserver.DateAndTime({
'Year': year,
'Month': month,
'Day': day,
'Hour': hour,
'Minute': minute,
'Second': second
});
}
}
} catch { }
打开不带参数的路径:
const url = 'http://127.0.0.1:4200/';
const p = '/SN/' + data.SerialNumber +'/'+data.Type+'/'+data.Name
+'/'+data.TestDate.Year+'/'+data.TestDate.Month+'/'+data.TestDate.Day
+'/'+data.TestDate.Hour+'/'+data.TestDate.Minute+'/'+data.TestDate.Second;
window.open(encodeURI(url+p));
如果不适用路由器,即如果只有单页面的话,可以使用:
// http://127.0.0.1:4200/asdas/asda/sdsa/sadas/SN/TBEA00RD410168/40002/001-default/2018/12/4/16/51/43
// http://127.0.0.1:4200/inasdasd.html?para=SN|TBEA00RD410168|40002|001-default|2018|12|4|16|51|43
async ngOnInit() {
this.searchType = 0;
try {
if (location.search && location.search.indexOf('para') >= 0) {
let qp: string = location.search;
qp = qp.replace('?para=', '');
const qps = qp.split('|');
if (qps.length >= 10) {
this.createParas(qps);
} else {
this.errorString = await this.translate.get('GLOBAL.URLE').toPromise();
}
} else {
if (location.href && location.pathname) {
const qp: string = location.pathname;
const qps = qp.split('/');
if (qps.length >= 10) {
const pars: string[] = [];
for (let i = 10; i >= 1; i--) {
pars.push(qps[qps.length - i]);
}
this.createParas(pars);
} else {
this.errorString = await this.translate.get('GLOBAL.URLE').toPromise();
}
}
}
} catch { }
}
private createParas(qps: string[]) {
const searchTypeName = qps[0];
if (searchTypeName === 'SN') {
this.searchType = 0;
} else if (searchTypeName === 'TID') {
this.searchType = 1;
} else if (searchTypeName === 'SN') {
this.searchType = 2;
}
this.searchParam = qps[1];
this.resultType = Number(qps[2]);
this.resultName = qps[3];
const year = Number(qps[4]);
const month = Number(qps[5]);
const day = Number(qps[6]);
const hour = Number(qps[7]);
const minute = Number(qps[8]);
const second = Number(qps[9]);
this.time = new rserver.DateAndTime({
'Year': year,
'Month': month,
'Day': day,
'Hour': hour,
'Minute': minute,
'Second': second
});
}