Cha*_*tra 3 typescript angular2-routing angular
嗯,在问这个问题之前我做了一些研究。
这是我的路
{ path: 'cart/:productIds', component: CartComponent }
Run Code Online (Sandbox Code Playgroud)
我想在点击购物车时传递productIds,如下所示在AppComponent中。我可以这样做吗?
goToCart() {
this.router.navigate(['/cart', this.productIds]);}
Run Code Online (Sandbox Code Playgroud)
我正在尝试像这样在 cartcomponent 的 ngOnit 中访问这些 ProductId
this.productIds = route.snapshot.params["productIds"];
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是在两个组件之间传递和访问 Int 数组的有效方法吗?
我们如何维护组件之间的状态(不确定这个词是否正确,我的背景是 ASP.NET)?
使用 aservice将 id 数组从一个组件保存到另一组件。这不是通过 url 传递它的安全方法。
创建这样的服务,
import { Injectable } from '@angular/core';
@Injectable()
export class AppMessageQueuService {
myIds: Array<Number>=[];
constructor() {
}
saveIds(produIds:any){
this.myIds = produIds;
}
retrieveIDs(){
return this.myIds;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此服务注入到两个组件中,并从第一个组件中保存它,例如,
this.appMsgService.saveIds(yourIds);
Run Code Online (Sandbox Code Playgroud)
然后在第二个组件中检索为,
this.appMsgService.retrieveIDs(yourIds);
Run Code Online (Sandbox Code Playgroud)
您可以将其注入到您的组件中,
constructor(
private appMsgService: AppMessageQueuService
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4555 次 |
| 最近记录: |