Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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_Variables_Url_Typescript - Fatal编程技术网

Javascript 在Angular 2中通过URL传递变量或设置变量

Javascript 在Angular 2中通过URL传递变量或设置变量,javascript,angular,variables,url,typescript,Javascript,Angular,Variables,Url,Typescript,我有一份用Angular2写的申请书。在一个组件中,有一个图表,您可以通过设置几个变量来调整它。我希望用户已经完全设置了这些变量已经由URL链接设置图表 例如:localhost:4200/app/chart?height=500;宽度=400;颜色=蓝色 在我的组件中,有一些变量称为: this.height: number; this.width: number; this.color: string; 这就是我的问题,如何在组件加载时,从URL链接准确地设置它们?(在init上) 我的第

我有一份用Angular2写的申请书。在一个组件中,有一个图表,您可以通过设置几个变量来调整它。我希望用户已经完全设置了这些变量已经由URL链接设置图表

例如:
localhost:4200/app/chart?height=500;宽度=400;颜色=蓝色

在我的组件中,有一些变量称为:

this.height: number;
this.width: number;
this.color: string;
这就是我的问题,如何在组件加载时,从URL链接准确地设置它们?(在init上)

我的第二个问题是,如何将这些变量传递到URL?例如,我有以下变量:

this.height: number = 500;
this.width: number = 400;
this.color: string = blue;
如何将它们发送到URL?使其看起来像:

localhost:4200/app/chart?height=500;width=400;color=blue`
我的路由文件:

import { Routes, RouterModule } from '@angular/router';

import { ChartComponent } from './chart/chart/index';

const appRoutes: Routes = [
  { path: 'app', component: AppComponent,
    children: [
      { path: 'chart', component: ChartComponent },
      { path: '', component: DashboardComponent }
    ]},

  // otherwise redirect to home
  { path: '**', redirectTo: '' }
 ];

export const routing = RouterModule.forRoot(appRoutes);

你能粘贴你的路线配置吗?我想你的答案就在
@angular/router
名称空间:)@A1rPun Done my friends把它放在哪里?在路由文件或我的图表组件中?在您使用它的组件中。别忘了从“@angular/router”导入
import{ActivatedRoute}否此使用当前URL路由路径如果要将这些变量发送到URL怎么办?不仅下载?@J.Doe
this.router.navigate(['/chart',this.height,this.width,this.color])
constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.params.subscribe(
      (params: any) => {
        this.height: number = params['height'];
        this.width: number = params['width'];
        this.color: string = params['color'];
      }
    );
  }