【发布时间】:2017-03-15 21:09:55
【问题描述】:
我有一个用 Angular2 编写的应用程序。在一个组件中,有一个图表,您可以通过设置几个变量来调整它。我希望用户已经通过 URL 链接设置了这些变量的完整图表。
例如:localhost:4200/app/chart?height=500;width=400;color=blue
在我的组件中,有一些变量叫做:
this.height: number;
this.width: number;
this.color: string;
这是我的问题,如何在组件加载时从 URL 链接准确设置它们? (在初始化时)
我的第二个问题是,如何将这些变量传递到 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 完成了我的朋友
标签: javascript angular variables url typescript