angular2路由器导航未正确加载组件

angular2路由器导航未正确加载组件,angular,angular-routing,angular-components,Angular,Angular Routing,Angular Components,我的代码如下所示: googleClick(): void { this._auth.login('google').subscribe( (data: any) => { console.log('google server data ' + JSON.stringify(data)); this.loginService.registerUser(data.email, data.name, '0').subscr

我的代码如下所示:

googleClick(): void {
    this._auth.login('google').subscribe(
        (data: any) => {
            console.log('google server data ' + JSON.stringify(data));
            this.loginService.registerUser(data.email, data.name, '0').subscribe(res => {
                if (res.status === '200') {
                    this.storage.store('user_token', res.data.user_token);
                    this.storage.store('user_email', data.email);
                    this.storage.store('user_img', data.image);
                    this.storage.store('user_first_name', res.data.user_name);
                    this.storage.store('user_id', res.data._id);
                    this.storage.store('user_paltform_login', 0);
                    this.router.navigate(['/home']);
                }

            });
        });
}
我一执行这段代码,它就会加载home(当前)路由和background login(过去)路由。它看起来很奇怪,如下所示:

app-routing.module.ts

const routes: Routes = [
  {path: '', redirectTo: '/login', pathMatch: 'full' },
  {path : 'home', component : HomeComponent },
  {path : 'login', component : LoginComponent},
  {path : 'foodtrucks', component : FoodComponent},
  {path : ':user_name/order-history', component : OrderHistoryComponent},
  {path : 'cart' , component : CartComponent},
  {path : 'payment', component : PaymentComponent},
  {path : ':order_id/order-details', component : OrderDetailsComponent},
  {path : 'food-info', component : FoodInfoComponent},
  {path : 'thank-you', component : ThankYouComponent}
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}
app.component.ts

@Component({
    selector: 'my-app',
    template: '<router-outlet></router-outlet>',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
}
@Component({
    selector: 'my-home',
    templateUrl : '../../template/home.html',
    styleUrls : ['../../css/home.css']
})


export class HomeComponent implements OnInit {
    food: any= [];
    itemList: any = [];
    user_name: any;
    user_email: any;
    user_image: any;
    myStyle: any = {
        width: '0px'
    };

    constructor(private router: Router, private _homeService: HomeService,
    private _storage: LocalStorageService, private winRef: WindowRef) {
        console.log('Window object', winRef.nativeWindow);
        // winRef.nativeWindow.alert('it is loaded');
    }

    ngOnInit(): void {
        this.user_name = this._storage.retrieve('user_first_name');
        this.user_email = this._storage.retrieve('user_email');
        this.user_image = this._storage.retrieve('user_img');
        this._homeService.getPopularItmes().subscribe(res => {
        if (res.status === '200') {
            let food = res.data;
            for (let value of food) {
                for (let item of value.item_list){
                    this.itemList.push(item);
                }
            }
        }
    });
}


    openNav(): void {
        console.log('open nav clicked');
        this.myStyle.width = '230px';
    };

    closeNav(): void {
        this.myStyle.width = '0px';
    };

    tabClicked(): void {
        this.router.navigate(['/foodtrucks']);
    };

    loadCart(): void {
        this.router.navigate(['/cart']);
    };
}
@Component({
    selector : 'my-login',
    templateUrl : '../../template/login.html',
    styleUrls : ['../../css/login.css']
})

export class LoginComponent {

    constructor(private router: Router, public _auth: AuthService,
        private storage: LocalStorageService, private loginService: LoginService) {
     }

    FBLogin(): void {
        this._auth.login('facebook').subscribe(
            (data: any) => {
              console.log(data);
            }
          );
        // this.router.navigate(['/home']);
    }

    googleClick(): void {
        this._auth.login('google').subscribe(
            (data: any) => {
              console.log('google server data ' + JSON.stringify(data));
              this.loginService.registerUser(data.email, data.name, '0').subscribe(res => {
                if (res.status === '200') {
                   this.storage.store('user_token', res.data.user_token);
                   this.storage.store('user_email', data.email);
                   this.storage.store('user_img', data.image);
                   this.storage.store('user_first_name', res.data.user_name);
                   this.storage.store('user_id', res.data._id);
                   this.storage.store('user_paltform_login', 0);
                   this.router.navigate(['/home']);
            }

    });
});

    }

}
login.component.ts

@Component({
    selector: 'my-app',
    template: '<router-outlet></router-outlet>',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
}
@Component({
    selector: 'my-home',
    templateUrl : '../../template/home.html',
    styleUrls : ['../../css/home.css']
})


export class HomeComponent implements OnInit {
    food: any= [];
    itemList: any = [];
    user_name: any;
    user_email: any;
    user_image: any;
    myStyle: any = {
        width: '0px'
    };

    constructor(private router: Router, private _homeService: HomeService,
    private _storage: LocalStorageService, private winRef: WindowRef) {
        console.log('Window object', winRef.nativeWindow);
        // winRef.nativeWindow.alert('it is loaded');
    }

    ngOnInit(): void {
        this.user_name = this._storage.retrieve('user_first_name');
        this.user_email = this._storage.retrieve('user_email');
        this.user_image = this._storage.retrieve('user_img');
        this._homeService.getPopularItmes().subscribe(res => {
        if (res.status === '200') {
            let food = res.data;
            for (let value of food) {
                for (let item of value.item_list){
                    this.itemList.push(item);
                }
            }
        }
    });
}


    openNav(): void {
        console.log('open nav clicked');
        this.myStyle.width = '230px';
    };

    closeNav(): void {
        this.myStyle.width = '0px';
    };

    tabClicked(): void {
        this.router.navigate(['/foodtrucks']);
    };

    loadCart(): void {
        this.router.navigate(['/cart']);
    };
}
@Component({
    selector : 'my-login',
    templateUrl : '../../template/login.html',
    styleUrls : ['../../css/login.css']
})

export class LoginComponent {

    constructor(private router: Router, public _auth: AuthService,
        private storage: LocalStorageService, private loginService: LoginService) {
     }

    FBLogin(): void {
        this._auth.login('facebook').subscribe(
            (data: any) => {
              console.log(data);
            }
          );
        // this.router.navigate(['/home']);
    }

    googleClick(): void {
        this._auth.login('google').subscribe(
            (data: any) => {
              console.log('google server data ' + JSON.stringify(data));
              this.loginService.registerUser(data.email, data.name, '0').subscribe(res => {
                if (res.status === '200') {
                   this.storage.store('user_token', res.data.user_token);
                   this.storage.store('user_email', data.email);
                   this.storage.store('user_img', data.image);
                   this.storage.store('user_first_name', res.data.user_name);
                   this.storage.store('user_id', res.data._id);
                   this.storage.store('user_paltform_login', 0);
                   this.router.navigate(['/home']);
            }

    });
});

    }

}
我注意到的一件事是,如果我删除了
googleClick()
中的所有内容,只保留
this.router.navigate(['/home'])那么这就可以了

在您的
googleClick()
方法上执行以下操作:

  • NgZone
    注入组件
  • 换行
    this.router.navigate(['/home'])进入NgZone
    运行
    方法
它将像:

this.ngZone.run(()=>this.router.navigate(['/home']))


这是一个基于github的解决方案。

你能显示你的路由器文件吗?你能用路由器插座显示html文件吗?@learner我在更新的问题中包含了必要的文件。我想你也必须显示HomeComponent和LoginComponent代码。@JaroslawK。我已经更新了问题中两个组件的代码