我有一個包含子網格項組件的父網格組件。我的大多數子網格項組件在單擊時導航到路由。但是,我有一個孩子需要在單擊時打開一個模態視窗而不是導航。
我可以向我的子組件傳遞對模板的參考#deactivateDialog嗎?當我需要模板本身時,
我當前的用法@Input route將其作為字串發送。
家長:
<app-grid-item route="deactivateDialog" (navigationType)="openDialog($event)"></app-grid-item>
//more app-grid-items
<ng-template #deactivateDialog>
//dialog markup
</ng-template>
openDialog(dialog) {
//opening dialog
}
孩子:
<div (click)="navigate()">
@Input() route;
@Output() navigationType = new EventEmitter();
navigate() {
this.navigationType.emit(this.route);
}
uj5u.com熱心網友回復:
您可以將導航邏輯移動到 TypeScript 檔案,并將 2 個引數傳遞給函式:
- 導航的路線(如果它不是應該打開對話框的專案)
- true/false(如果您應該導航,則為 true,如果您必須打開對話框,則為 false)
<ng-template #deactivateDialog>
//dialog markup
</ng-template>
<app-grid-item (click)="custom_function('route_to_navigate', true)"></app-grid-item>
現在在 TypeScript 檔案中創建custom_function(),它將檢查您是應該導航還是應該打開對話框。
custom_function(route_to_navigate, is_navigating){
if (is_navigating){
this.router.navigate([route_to_navigate]);
} else {
// Open dialog
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/349440.html
標籤:javascript 有角的 打字稿
