简体   繁体   English

如何在TypeScript中向我的URL添加查询参数?

[英]How do I add query parameters to my url in TypeScript?

After I click on a button, I want to redirect to a different page. 单击按钮后,我想重定向到另一个页面。 So I have the following function: 所以我有以下功能:

//this redirects me correctly
click() {
    window.location.href = 'download/' + this.some.string.toLowerCase();
    console.log(this.client.displayName);
  }

However I would like my url to look something like this: 但是我希望我的网址看起来像这样:

toggleDownload() {
    const someValue = this.client.getValue();
    window.location.href = 'download/' + this.client.displayName.toLowerCase()+'key='+value;
    console.log(this.client.displayName);
  }

If the query parameters are presented, I do something based on this, if not, I don't. 如果提供了查询参数,则我将基于此执行某些操作,否则,我不会。 Is this the correct way to add query parameters? 这是添加查询参数的正确方法吗? Do I just append them as string? 我只是将它们附加为字符串吗?

Use template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals 使用模板文字: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals

This will remove the need to manually concatenate the substrings into one, as shown below: 这样就无需手动将子字符串连接为一个,如下所示:

toggleDownload(): void {
    const someValue = this.client.getValue();
    const urlWithParams = `download/${this.client.displayName.toLowerCase()}+key=${value}`;
    window.location.href = urlWithParams;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM