我的module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule,Router } from '@angular/router';
import { AppComponent } from './crud/app.component';
import { Profile } from './profile/profile';
import { Mainapp } from './demo.app';
import { Navbar } from './header/header';
// import {ToasterModule, ToasterService} from 'angular2-toaster';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [ BrowserModule,FormsModule, ReactiveFormsModule ,
RouterModule.forRoot([
{ path: '', component:AppComponent},
{ path: 'login', component:AppComponent},
{ path: 'profile', component:Profile}
]) ],
declarations: [ AppComponent,Mainapp,Navbar,Profile ],
bootstrap: [ Mainapp ]
})
export class AppModule {
}在这里,我想在每次改变路线时从main.ts调用一个函数,我如何做that.Can --任何人都请帮助me.Thanks。我的mainapp.ts
export class Mainapp {
showBeforeLogin:any = true;
showAfterLogin:any;
constructor( public router: Router) {
this.changeOfRoutes();
}
changeOfRoutes(){
if(this.router.url === '/'){
this.showAfterLogin = true;
}
}
}我想为每一个路线的改变调用这个changeofRoutes(),怎么做呢?有谁能帮我吗?
发布于 2017-02-25 07:55:03
您可以像这样从主activate调用router-outlet方法
<router-outlet (activate)="changeOfRoutes()"></router-outlet>每次路线改变的时候都会打电话。
更新-
实现同样目标的另一种方法是订阅路由器事件,甚至可以根据导航状态筛选出它们,例如,开始和结束-
import { Router, NavigationEnd } from '@angular/router';
@Component({...})
constructor(private router: Router) {
this.router.events.subscribe((ev) => {
if (ev instanceof NavigationEnd) { /* Your code goes here on every router change */}
});
}发布于 2017-02-25 08:21:05
您可以订阅路由器的NavigationEnd事件,并使用urlAfterRedirects方法检索URL。
urlAfterRedirects,因为您似乎需要有条件地使用showAfterLogin。/test-page重定向到/,并且依赖于router.url。在这种情况下,应用程序将被重定向到/,但是router.url将返回/test-page,在这里, already comes ('/test-page' != '/')。简单地,在构造函数中进行以下更改:
export class Mainapp {
showBeforeLogin:any = true;
showAfterLogin:any;
constructor(public router: Router) {
this.changeOfRoutes();
this.router.events
.filter(event => (event instanceof NavigationEnd))
.subscribe((routeData: any) => {
if(routeData.urlAfterRedirects === '/') {
this.showAfterLogin = true;
}
});
}
}发布于 2018-03-16 05:37:34
您可以在下面这样的路由中调用指令:
{ path: 'dashboard', component: DashboardComponent , canActivate: [AuthGuard] },您的AuthGuard组件如下所示:
auth.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot,
RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
{
if (localStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/home'], { queryParams: { returnUrl:
state.url }});
return false;
}
}您应该在AuthGuard文件中导入app.module.ts组件,并在提供程序中提供:
app.module.ts:
......... Your code..........
import { AuthGuard } from './_guards/index';
..........Your code..............
providers: [
AuthGuard,
........
],https://stackoverflow.com/questions/42453375
复制相似问题