Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular 2中设置URL参数_Javascript_Angular_Url_Typescript_Parameters - Fatal编程技术网

Javascript 在Angular 2中设置URL参数

Javascript 在Angular 2中设置URL参数,javascript,angular,url,typescript,parameters,Javascript,Angular,Url,Typescript,Parameters,我正在使用以下代码: this.router.navigate(['/app/chart', {chartColor: this.color, chartWidth: this.width}]); 它将我的URL设置为: http://localhost/app/chart;chartColor=blue;chartWidth=600 很好但是当我调用该函数(将变量发送到URL)时,我的组件被重新加载。我真的不想要它 我只想将变量发送到URL 还有其他更好的方法吗?我真的不知道angular

我正在使用以下代码:

this.router.navigate(['/app/chart', {chartColor: this.color, chartWidth: this.width}]);
它将我的URL设置为:

http://localhost/app/chart;chartColor=blue;chartWidth=600
很好但是当我调用该函数(将变量发送到URL)时,我的组件被重新加载。我真的不想要它

我只想将变量发送到URL
还有其他更好的方法吗?

我真的不知道angular2是否足够好,但对于角度“1”,我使用以下方法:

首先我得到“基本url”(协议+主机名)

然后,我通过

urlVar = "?chartColor=blue&chartWidth=600"
最后,我将字符串(url)写入浏览器的历史记录:

window.history.pushState('name', '', Tbaseurl + urlVar);
这会将url保存到用户的浏览器历史记录中(因此,如果他们想返回页面,可以这样做),并且无需刷新页面即可更新url


同样,我也不能给你答案,因为我对angular2的了解还不够好,无法帮助你解决问题,但我希望这能提供足够的信息来帮助你解决问题

你正在使用路由器从一个组件导航到另一个组件。导航,因此你应该期望组件重新加载。你需要通过URL传递你的参数吗?@YounesM不,我只需要将它发送到同一组件的URL。我想让用户能够粘贴此URL,并且此组件中的图表将宽度设置为
600
,并且将为
blue
。所以用户不必自己动手。谢谢,我会试试的@henrythiuerryt运气好吗?
window.history.pushState('name', '', Tbaseurl + urlVar);