angular - 在角度分量中使用布尔值
问题描述
我尝试了一些有角度的手。我尝试使用相同的组件进行编辑以及创建产品。我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;
});
}
}
解决方案
我喜欢创建 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;
}
推荐阅读
- json - 仅打印数组属性中的对象的一个属性以及与 jq 中的数组属性同级的属性
- android - 为什么我的图标形状不像 Android 上的其他图标?
- javascript - Javascript / React Native - 如何在两个不同的对象数组之间进行交叉检查?
- mysql - phpmyadmin 这是索引大小吗?
- python - 我们如何将列表作为函数中的参数?
- ruby - 初始化方法中的'self.class.class_method_name'有什么作用?
- java - 创建没有重复的随机整数数组
- angular - Nativescript queryParamsMap 在返回时不起作用
- visual-studio-2019 - 如何在 Visual Studio 2019 中启用 Microsoft 文档弹出窗口,如 NetBeans 中的 java 文档
- typescript - 来自另一个 ts 文件的打字稿导入功能 - 文件不是模块错误