gpt4 book ai didi

angular - 在父 ngOnInit 完成之前调用的子组件

转载 作者:太空狗 更新时间:2023-10-29 18:07:55 25 4
gpt4 key购买 nike

首先,在ngOnInit()中制作表设置数据,并设置到settingA变量中。

此设置变量绑定(bind)到 html 中的设置。

我想使用 settingA 在子组件中制作表格。

但是子组件 ngOnInit 在父组件的 ngOnInit 完成之前被调用。

因此在子组件中设置变量是未定义的。

我需要你的建议。

这是父组件[网格序列化.component.ts]

@Component({
selector: 'grid-serialize',
templateUrl: './grid-serialize.component.html',
styles: []
})
export class GridSerializeComponent implements OnInit, OnChanges {
tableSetting: TableSetting;
settingA: TableSetting;
rows: Bom[] = [];
cols: ColumnSettings[] = [];
userId: string;
url: string;
area: string;

constructor(
private router: Router,
private tableService: TableService,
private localStorageService: GridService
) {}

ngOnInit(): void {

this.userId = 'user01';

this.url = this.router.url;

const areaA = 'upper';
const rowDataPathA = 'aaa/aaa/data.json';
const columnDataPathA = 'bbb/bbb/column.json';
const reorderableColumnsA = false;
const resizableColumnsA = false;
const paginatorA = true;
const paginatorRowsA = 10;

this.tableService.setColumnDataPath(columnDataPathA);
this.tableService.setRowDataPath(rowDataPathA);
this.tableService.getRowData().subscribe((rowData) => {
console.log('---- getRowData() -----');
this.rows = rowData;
console.log('this.rows = ', this.rows);
this.localStorageService
.fetchColumns(this.userId, this.url, areaA)
.subscribe((columnData) => {
if (columnData && columnData.length > 0) {
this.cols = columnData;
this.settingA = new TableSetting(
this.userId,
this.url,
areaA,
resizableColumnsA,
reorderableColumnsA,
this.cols,
this.rows,
paginatorA,
paginatorRowsA,
columnDataPathA
);
} else {
this.tableService
.getColumnSetting()
.subscribe((initData) => {
console.log('*** getColumnSetting() ***');
this.cols = initData;
console.log('this.cols = ', this.cols);
this.settingA = new TableSetting(
this.userId,
this.url,
areaA,
resizableColumnsA,
reorderableColumnsA,
this.cols,
this.rows,
paginatorA,
paginatorRowsA,
columnDataPathA
);
});
}
});
});
}
}

settingA 绑定(bind)到子组件中的设置变量

这是父组件.html[网格序列化.component.html]

<grid-table [setting]="settingA"></grid-table>

这是子组件[表格.组件.ts]

@Component({
selector: 'grid-table',
templateUrl: './table.component.html',
styles: []
})
export class TableComponent implements OnInit, OnDestroy {
@ViewChild(Table) tableComponent: Table;
@Input() setting: TableSetting;
@Input() clickedEvent: string;

area: string;
paginatorRows: number;
paginator: boolean;
resizable: boolean;
reordable: boolean;
userId: string;
msgs: Message[] = [];
checkAll: Boolean = false;
url: string;
defaultCols: ColumnSettings[] = [];
cols: ColumnSettings[] = [];
rows: Bom[] = [];
private subscription: Subscription;

constructor(
) {}

ngOnInit(): void {
⬇︎⬇︎⬇︎⬇︎⬇︎ this.setting is undefiend  ⬇︎⬇︎⬇︎⬇︎⬇︎
console.log('this.setting = ', this.setting);
this.tableComponent.reset();

this.resizable = true;
this.reordable = true;

this.resizable = this.setting.resizableColumns;

this.reordable = this.setting.reorderableColumns;

this.paginator = this.setting.paginator;

this.paginatorRows = this.setting.paginatorRows;

this.service.setColumnDataPath(this.setting.columnDataPath);

this.cols = this.setting.columnData;

this.rows = this.setting.rowData;
}
}

这是子组件html[表格.组件.html]

<p-table [paginator]="paginator" [rows]="paginatorRows" [columns]="cols" [value]="rows" [resizableColumns]="resizable" [reorderableColumns]="reordable">
<ng-container>
<ng-container *ngTemplateOutlet="gridTableHeader"></ng-container>
</ng-container>
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns" pReorderableColumn pResizableColumn [ngStyle]="col.style" [pSortableColumn]="col.field">
<span *ngIf="col.type === 'text'">
{{ col.caption | translate }}
<p-sortIcon [field]="col.field"></p-sortIcon>
</span>
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr>
<td *ngFor="let col of columns">
<span *ngIf="col.type === 'text'">
{{ rowData[col.field] }}
</span>
</td>
</tr>
</ng-template>
</p-table>

最佳答案

您可以添加一个 *ngIf 来检查它是否已加载,一旦 settingA 值可用,您的子组件将被渲染

<grid-table *ngIf="settingA" [setting]="settingA" [clickedEvent]="event"></grid-table>

关于angular - 在父 ngOnInit 完成之前调用的子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48911152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com