【问题标题】:Unable to get the nextstate(next url ) in CanDeactivate router guard无法在 CanDeactivate 路由器防护中获取下一个状态(下一个 url)
【发布时间】:2017-10-11 14:54:19
【问题描述】:

我为 CanDeactivate 功能添加了以下路由器保护,以验证未保存的更改,这对我来说工作正常

导出接口 CanComponentDeactivate { canDeactivate: () => 可观察 |承诺 |布尔值; }

@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
    constructor( private eventService: EventsService) { }
  canDeactivate(component: CanComponentDeactivate, route?: ActivatedRouteSnapshot,
    currentState?: RouterStateSnapshot,
    nextState?: string) {
      let sub = new Subject();
      if (BaseApiService.callStack.length || UnsavedData.isThereUnSavedData) {
          this.eventService.triggerEvent(CONST.SHOW_DATA_LOSS_MODAL, {
              showDataLossModal: true,
              sub : sub
          });
      }
      return BaseApiService.callStack.length || UnsavedData.isThereUnSavedData ? false : true;
  }
}

但是当我检测到是否有任何未保存的更改时,我会显示一个带有“是”和“否”按钮的对话框。

如果用户单击“是”,我必须清除未保存的更改并重定向到用户单击的 URL。为此,我必须检查用户想要在 CanDeactivate 函数上重定向的 URL。

nextState?: string 但是每次我得到这个 undefined。

如果我做了任何更改,请纠正我。

【问题讨论】:

    标签: angular routing angular2-routing state


    【解决方案1】:

    你需要使用,

    nextState?:RouterStateSnapshot

    下面是接口定义,

    interface CanDeactivate<T> { 
      canDeactivate(component: T, 
      currentRoute: ActivatedRouteSnapshot, 
      currentState: RouterStateSnapshot, 
      nextState?: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean
    }
    

    检查定义here。

    更新

    查看下面的完整示例,

    @Component({
      selector: 'my-app',
      template: `<h1>Hello {{name}}</h1>
      <hr />
        <a routerLink="/home" >Home</a>
         <a routerLink="/other" >Other</a>
      <hr />
      <router-outlet></router-outlet>
      `
    })
    class AppComponent { name = 'Angular'; }
    
    @Component({
      template: `<h1>Home</h1>
      
      <a routerLink="/other" >Go to Other from Home</a>
      `
    })
    class HomeComponent {
    }
    
    @Component({
      template: `<h1>Other</h1>
      `
    })
    class OtherComponent {
    }
    
    @Injectable()
    class CanDeactivateHome implements CanDeactivate<HomeComponent> {
      canDeactivate(
        component: HomeComponent,
        currentRoute: ActivatedRouteSnapshot,
        currentState: RouterStateSnapshot,
        nextState: RouterStateSnapshot
      ): Observable<boolean>|Promise<boolean>|boolean {
       
       console.log(component);
       console.log(currentRoute);
       console.log(currentState);
       console.log(nextState);
       
       return true;
      }
    }
    
    const appRoutes: Routes = [
      { path: '',   redirectTo: '/home', pathMatch: 'full' },
      { path: 'home',  component: HomeComponent, canDeactivate: [CanDeactivateHome] },
      { path: 'other',  component: OtherComponent }
    ];
    
    @NgModule({
      imports:      [ BrowserModule, RouterModule.forRoot(appRoutes)],
      declarations: [ AppComponent, HomeComponent, OtherComponent ],
      providers: [CanDeactivateHome],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
    

    检查这个Plunker!!

    【讨论】:

    • @Rhushikesh,我用 Plunker 添加了一个简单的例子,你可以在控制台日志中看到下一个状态是打印时,当你去 Home 以外的路由时,你能检查你是否在做类似的事情,如果没有你可以编辑 Plunker 来重现你的问题,那么我可以帮忙,干杯!!
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多