angular - Ionic 4 - 从页面添加/删除按钮的最简单方法
问题描述
我正在使用页面变量从我的页面中添加/删除组件。
这听起来很奇怪,但它确实有效。
我的页面.html
<pop-product *ngIf="this.pop_product_open" [data]="this.pop_product_data"></pop-product>
我的页面.ts
public pop_product_open:boolean = false;
public pop_product_data:any = null;
...
async openPopProduct(product){
this.pop_product_open = true;
this.pop_product_data = product;
}
是否有任何其他/最简单/更好的方法可以从页面中添加/删除组件?
我在问,因为我将有大约 15 个组件,而且以这种方式管理大量变量的代码会变得多么混乱,这很可怕。
解决方案
如果有两个以上的页面,您可以使用这样的东西。
首先,您需要有一个功能来选择要显示的组件。
例子.ts
selectedPage: string = 'DefaultPage';
exampleFunction(page){
this.selectedPage = page;
}
然后,将显示该组件。默认情况下,如果特定组件尚不存在,您可以显示一些示例数据 ( !selectedPage
)。
例子.html
<ion-content>
<map *ngIf="selectedPage == 'Map'"></map>
<offers *ngIf="selectedPage == 'Offers'"></offers>
<restaurants *ngIf="selectedPage == 'Eat'"></restaurants>
<emergencies *ngIf="selectedPage == 'Emergencies'"></emergencies>
<drink *ngIf="selectedPage == 'Drink'"></drink>
<spa *ngIf="selectedPage == 'Spa'"></spa>
<attractions *ngIf="selectedPage == 'Attractions'"></attractions>
<beaches *ngIf="selectedPage == 'Beaches'"></beaches>
<ion-row *ngIf="!selectedPage || selectedPage == 'DefaultPage'">
...
推荐阅读
- json - Flutter 获取 JSON 并返回结果
- react-native - 如何在本机反应中从所有屏幕中删除标题(stackNavigator)
- javascript - React.js 无法使用来自 useState 的 .map 遍历数组
- python - Plotly with python - 折线图取消全选
- excel - 电子表格和直接输入的结果不同,可能是由于 .specialcells(xlCellTypeBlank)?
- python - 如何在数据框上映射和创建与其他 2 列中的值相对应的新列
- bash - Bitnami Nginx Docker 映像上 cp 命令的权限被拒绝
- java - Maven 多模块项目不会因编译错误而失败
- mongodb - 使用 mongodb 聚合框架计算数组中的相似项
- reactjs - 将正确的属性传递给弹出通知