首页 > 解决方案 > 在角度分量中使用布尔值

问题描述

我尝试了一些有角度的手。我尝试使用相同的组件进行编辑以及创建产品。我if在组件中使用了一条语句来区分编辑和创建。但是if当请求 url 为http://localhost:4200/product/Edit/Redmond?edit=true. 我有处理路由参数的代码。为简洁起见,我将其删除。我不确定我在这里缺少什么

export class ProductEditComponent implements OnInit {
isEdit= false;
uiProduct:ProductModel;
ngOnInit() {
this.activatedRoute.queryParams.subscribe(queryParams=>{
  this.isEdit=queryParams['edit']      
  console.log(this.isEdit); // it's true here for the Edit Route
})
if (this.isEdit ==true) {      
  this.productService.
    GetProductByName(this.name)
    .subscribe((response: ProductModel) => {
      this.uiProduct = response;          
    });
}
}

标签: angulartypescript

解决方案


我喜欢创建 2 个路由器,并加载相同的组件:

 const routes: Routes = [
      { path: 'edit/:id', component: RequestReportComponent },
      { path: 'add', component: RequestReportComponent },
     ];

并在组件中,以这种方式检查:

isEditMode:boolean;
  constructor(private route: ActivatedRoute) {
             this.isEditMode = this.route.snapshot.params.id;
   
  }

推荐阅读