如何在TypeScript中将查询参数添加到url?
单击按钮后,我想重定向到另一个页面。所以我有以下功能:如何在TypeScript中将查询参数添加到url?,typescript,Typescript,单击按钮后,我想重定向到另一个页面。所以我有以下功能: //this redirects me correctly click() { window.location.href = 'download/' + this.some.string.toLowerCase(); console.log(this.client.displayName); } 但是,我希望我的url看起来像这样: toggleDownload() { const someValue = th
//this redirects me correctly
click() {
window.location.href = 'download/' + this.some.string.toLowerCase();
console.log(this.client.displayName);
}
但是,我希望我的url看起来像这样:
toggleDownload() {
const someValue = this.client.getValue();
window.location.href = 'download/' + this.client.displayName.toLowerCase()+'key='+value;
console.log(this.client.displayName);
}
如果提供了查询参数,我会基于此进行操作,如果没有,我不会。这是添加查询参数的正确方法吗?我只是将它们作为字符串附加吗?使用模板文本:
这将消除手动将子字符串连接成一个字符串的需要,如下所示:
toggleDownload(): void {
const someValue = this.client.getValue();
const urlWithParams = `download/${this.client.displayName.toLowerCase()}+key=${value}`;
window.location.href = urlWithParams;
}
TypeScript编译为JavaScript的可能副本;在TypeScript中需要做的任何事情,都可以使用JavaScript的答案。