2017-06-18 62 views
0

我正試圖在退出屏幕之前提醒用戶。如果他們按「不」,那麼它不應該破壞。如果他們按下「確定」,然後進行銷燬操作。ngOnDestroy在退出之前發出警告用戶

ngOnDestory是否有在ngOnDestory之前發生的事件?例如ngOnBeforeDestroying?

我目前正在與角4

+0

搜索canDeactivate guard.It可以讓你控制導航從特定page.So假設的地方有一個網頁,其中允許用戶填寫表單的用戶的後半段填滿一個form.So試圖去到另一個頁面,比我們可以使用這個警衛來確認用戶他是否真的想要導航,因爲他的表單數據在導航時會丟失。 – pritesh

回答

2

開發是的,你應該使用canDeactivate路線後衛。

創建一個注射服務

@Injectable() 
class CanDeactivateService implements CanDeactivate<TeamComponent> { 


    canDeactivate(
    component: TeamComponent, 
    currentRoute: ActivatedRouteSnapshot, 
    currentState: RouterStateSnapshot, 
    nextState: RouterStateSnapshot 
): Observable<boolean>|Promise<boolean>|boolean { 
    return component.isDirty ; 
} 
} 

這可以用來確定是否頁面可以被摧毀或沒有。

這應當在路線中被配置爲

RouterModule.forRoot([ 
     { 
     path: '..', // path 
     component: Comp, // name of the component 
     canDeactivate: [CanDeactivateService] 
     } 
    ]) 
    ], 

Read more here...

Angular's Official Demo

這可以通過動態元件裝載也可實現。按照步驟here

相關問題