Angular redirectTo:将数据传递到重定向路由

问题描述 投票:0回答:1

有没有办法仅在从 oldpath 重定向时将“数据”传递到 home 组件,而不是从 '' 重定向?

const routes: Routes = [
      {path : '', redirectTo:'home'},
      {path : 'oldPath', redirectTo:'home', data: {defaultAction: 'oldPath'}},
      {path : 'home', component : homeComponent, resolve:{shapeLoaded:integrationResolver}}
    ];

我不想将主路径与另一个条目重复。

angular angular-routing redirecttoroute
1个回答
0
投票
  1. 您可以使用“状态”中的导航将“数据”传递到任何路由器 property”而不是像this SO

    那样使用路由器中自己的数据
  2. 或者您可以使用服务并在之前存储/清除所需的值 导航。

  3. 或者您可以存储“上一页”,如显示此链接

    简化一下,您创建一个服务 - 在链接中使用主题,但是您 可以简单地使用变量-

    @Injectable({providedIn:'root')
    export class UrlService {
      public previousUrl: string="";
      public currentUrl: string="";
      constructor() { }
    }
    

    在你的 main.app 中

      constructor(private router: Router,
                  private urlService: UrlService) {}
    
      ngOnInit() {
        this.router.events.pipe(
          filter((event) => event instanceof NavigationEnd)
        ).subscribe((event: NavigationEnd) => {
          this.urlService.previousUrl=this.urlService.currentURL 
          this.urlService.currentUrl = event.url;
        });
      }
    
© www.soinside.com 2019 - 2024. All rights reserved.