【发布时间】:2020-01-19 02:41:28
【问题描述】:
我有两个不同的组件 - Component1 和 Component2 以及一个 Service1。
当我使用服务(服务!)在 Component1(HomeComponent)中单击一个图像(产品列表中的一个产品)时,我在服务中设置了特定的 productId。
我想使用特定的 productId 使选项卡(产品列表表示为选项卡)在 Component2(产品组件)中处于活动状态。
我可以得到任何帮助来完成这项工作吗?
我只使用引导选项卡。
HomeComponent 模板:
<div class="icon center-content"
(click)="getProductInfo(productsCategory.ProductCategoryID)">
<img class="cat-image mb-4" [src]="productsCategory.ImagePath" alt="Place image title" *ngIf="!isImageLoading; else noImageFound">
<ng-template #noImageFound>
<img src="assets/img/fallbackImage.png" alt="Fallbackimage">
</ng-template>
</div>
主页组件:
getProductInfo(id){
console.log(id);
this.appService.getProductInfoHome(id);
this.router.navigateByUrl('home/products');
}
ProductComponent 模板:
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a id="tab-A" [routerLink]="['#pane-A']" routerLinkActive="router-link-active" [class.active]="tab == 'tab-A'" class="nav-link" data-toggle="tab" role="tab">Product One</a>
</li>
<li class="nav-item">
<a id="tab-B" [routerLink]="['#pane-B']" routerLinkActive="router-link-active" [ngClass]="{'active': tab == 'tab-B'}" class="nav-link" data-toggle="tab" role="tab">Product Two</a>
</li>
产品组件:
//To get the tab details
getTabDetails() {
this.tab = this.appService.setActiveTab();
}
ngOnInit() {
//Stores the product ID from service
this.selectedProductID = this.appService.getProductID();
console.log(this.selectedProductID);
//Will call API and subscribes in the
this.appService.getProductInfoHome(this.selectedProductID);
this.tab = this.appService.getActiveTab();
console.log(this.tab);
this.products = this.appService.firstProductData;
console.log(this.products);
}
应用服务:
//set Id
setProductID(id) {
this.selectedId = id;
}
//Get ID
getProductID(){
return this.selectedId;
}
//Get Product Info from home
getProductInfoHome(id){
this.getProductInfoById(id).subscribe(
data=>{
this.firstProductData = data;
console.log(this.firstProductData);
if(this.firstProductData){
this.setActiveTab();
console.log(this.selectTab);
}
},
error=>{
console.log(error);
}
)
}
//To set the tab active
setActiveTab(): any {
for (let cat of prodConfig.productCategory) {
if (cat.ProductCategoryID ===
this.firstProductData.ProductCategoryID) {
this.selectTab = cat.ProductTabID;
}
}
}
//To get the active tab
getActiveTab():any{
if(this.selectTab)
return this.selectTab;
}
//To get the product type and products byId
getProductInfoById(productId) {
const data = {
productCategoryId: productId
};
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
const reqData = JSON.stringify(data);
return
this.http.post('/api/product/getinfobyid', reqData,
httpOptions).pipe(
catchError((error: HttpErrorResponse) => {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it
accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.log(error.error);
}
return observableThrowError(error.error);
}));
}
【问题讨论】:
-
在此处添加一些代码,您到目前为止尝试过的内容以及代码的外观
-
请提供一些代码。
-
已经添加了代码,我尝试到现在。
标签: angular typescript