Angular 2生命周期挂钩不执行,构造函数也不执行
我正在尝试调用ngOnInit方法中的服务。无论如何,没有调用任何生命周期方法或构造函数。所以我猜组件并没有被构造。但是怎么做呢?呈现页面时,我在MainFrameModule中呈现了主模块: 下面是应用程序模块图 我试图在MainFrameComponent和/或Home component内部调用它们。在这两种情况下都没有任何结果。 这里是MainFrameModule路由Angular 2生命周期挂钩不执行,构造函数也不执行,angular,routing,lifecycle,Angular,Routing,Lifecycle,我正在尝试调用ngOnInit方法中的服务。无论如何,没有调用任何生命周期方法或构造函数。所以我猜组件并没有被构造。但是怎么做呢?呈现页面时,我在MainFrameModule中呈现了主模块: 下面是应用程序模块图 我试图在MainFrameComponent和/或Home component内部调用它们。在这两种情况下都没有任何结果。 这里是MainFrameModule路由 export const mainFrameRoutes:Routes = [ { path: 'main-f
export const mainFrameRoutes:Routes = [
{ path: 'main-frame', component : MainFrameComponent,
children : [
{ path:'', redirectTo : '/main-frame/home',pathMatch:'full'}
,{ path:'home', component : HomeComponent}
,{ path: 'products',component:ProductsComponent,loadChildren :'app/components/products/products.module#ProductsModule'}
,{ path:'account', loadChildren : 'app/components/account/account.module#AccountModule'}
,{ path:'billing',component:BillingComponent, loadChildren : 'app/components/billing/billing.module#BillingModule'}
,{ path:'affiliate',component: AffiliateComponent, loadChildren : 'app/components/affiliate/affiliate.module#AffiliateModule'}
]
}
];
主框架组件:
import { Component,HostBinding,Input,OnInit } from '@angular/core';
import { User } from '../../entities/user';
import { LimeProxiesService } from '../../services/limeproxies.service';
@Component({
selector : 'main-frame',
templateUrl : 'app/templates/main-frame/main-frame.template.html'
})
export class MainFrameComponent implements OnInit {
@HostBinding('class') mainFrameClass = 'page page--one-col';
@Input() user:User;
private errorMessage;
constructor(private limeProxiesService:LimeProxiesService){
console.log("in constructor");
}
ngOnChanges(){
console.log("ngOnChanges called");
}
ngAfterContentInit(){
console.log("ngAfterContentInit called");
}
ngAfterViewInit(){
console.log("ngAfterViewInit called");
}
ngOnDestroy(){
console.log("ngOnDestroy called");
}
ngOnInit(){
console.log("ngOnInit called");
this.setUser();
}
setUser(){
this.limeProxiesService.getUserProfile()
.subscribe(
//success
user => this.user = user,
//error
error => this.errorMessage = <any>error
//completed
);
}
}
import { Component,HostBinding,OnInit,Input } from '@angular/core';
import { HomeSidebarComponent } from './home-sidebar.component';
import { HomeMainContentComponent } from './home-main-content.component';
import { User } from '../../entities/user';
import { LimeProxiesService } from '../../services/limeproxies.service';
@Component({
selector: '.home-component',
templateUrl: 'app/templates/home/home.template.html'
})
export class HomeComponent implements OnInit {
@HostBinding('class') homeClass = 'page__content-inner';
@Input() user:User;
private errorMessage;
constructor(private limeProxiesService:LimeProxiesService){
}
ngOnChanges(){
console.log("ngOnChanges called");
}
ngAfterContentInit(){
console.log("ngAfterContentInit called");
}
ngAfterViewInit(){
console.log("ngAfterViewInit called");
}
ngOnDestroy(){
console.log("ngOnDestroy called");
}
ngOnInit(){
console.log("ngOnInit called");
this.setUser();
}
setUser(){
this.limeProxiesService.getUserProfile()
.subscribe(
//success
user => this.user = user,
//error
error => this.errorMessage = <any>error
//completed
);
}
}
从'@angular/core'导入{Component,HostBinding,Input,OnInit};
从“../../entities/User”导入{User};
从“../../services/limeproxies.service”导入{LimeProxiesService};
@组成部分({
选择器:'主框架',
templateUrl:'app/templates/main frame/main frame.template.html'
})
导出类MainFrameComponent实现OnInit{
@HostBinding('class')mainFrameClass='page page--一列';
@输入()用户:用户;
私人错误信息;
构造器(专用limeProxiesService:limeProxiesService){
console.log(“在构造函数中”);
}
ngOnChanges(){
log(“称为ngOnChanges”);
}
ngAfterContentInit(){
log(“调用了ngAfterContentInit”);
}
ngAfterViewInit(){
log(“调用ngAfterViewInit”);
}
恩贡德斯特罗(){
log(“称为Ngondestory”);
}
恩戈尼尼特(){
log(“名为ngOnInit”);
这是setUser();
}
setUser(){
this.limeproxiseservice.getUserProfile()文件
.订阅(
//成功
user=>this.user=user,
//错误
error=>this.errorMessage=我遇到了完全相同的问题此处使用webpack捆绑的问题相同我遇到了完全相同的问题此处使用webpack捆绑的问题相同