Angular 4 router.navigate没有正确处理路由

时间:2017-11-29 23:42:57

标签: javascript angular

我正在使用Angular 4路由器。在index.html中我有:

<base  href= "/">

我有一个包含以下内容的app.component.ts:

<div class="content-wrapper" routerLink=“/home” routerLinkActive="active">
    <div class="centered-content">
        <div class=“home-block">                        
                <router-outlet></router-outlet>
        </div>
    </div>
</div>

app-routing.module.ts包含以下内容:

const appRoutes: Routes = [
    {
        path: "home",
        component: HomeComponent,
        canActivate: [IsAuthenticatedGuard],
    },
    { path: "",
        redirectTo: "/home",
        pathMatch: "full"
    },
    { path: "**",
        redirectTo: "/home",
        pathMatch: "full"
    },
];

当用户点击转到新/另一个标签并返回时,我试图导航到“/ home”路线。为此,我正在实现@HostListener,如下所示:

@HostListener("window:focus", ["$event"])
onFocus(event: any): void {
    // Route to home page to run the activate Guard

    this.router.navigate( ["/home"], {relativeTo: this.route})
        .then((value) => console.log("Value " + value))
        .catch((error) => console.log("Error " + error));
}

HostListener正在运行,但.router.navigate ..不是

返回的值(.then((value)=&gt; ..)总是为null,除非我放入“”或“root / home”分别触发空路径或通配符路径。有人能告诉我我做错了什么以及如何使用相对路径导航到当前页面?

0 个答案:

没有答案