相关疑难解决方法(0)

当Injectable类被实例化时,不会调用ngOnInit

为什么ngOnInit()Injectable课程解决后没有打电话?

import {Injectable, OnInit} from 'angular2/core';
import { RestApiService, RestRequest } from './rest-api.service';

@Injectable()
export class MovieDbService implements OnInit {

    constructor(private _movieDbRest: RestApiService){
        window.console.log('FROM constructor()');
    }

    ngOnInit() {
         window.console.log('FROM ngOnInit()');
    }

}
Run Code Online (Sandbox Code Playgroud)

控制台输出

FROM constructor()
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

175
推荐指数
4
解决办法
9万
查看次数

在router.navigate之后没有调用ngOnInit

我的Angular应用程序突然没有调用ngOnInit(),router.navigation()这意味着我的组件无法正确加载.我认为这可能是由于我做出的一些改变,但恢复改变并没有解决问题.

正常导航导致组件无法正确加载的示例; 此页面由以下代码列表导航 this.router.navigate(['/result', this.params.data._id]);:

组件未正确加载

重新加载页面,组件正确加载:

组件正确加载

以下是我的一些代码清单,

app.module.ts

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    AgGridModule.withComponents(
            [SampleResultButtonComponent]
        ),
    ChartsModule
  ],
  declarations: [
    AppComponent,

    LoginComponent,
    LogoutComponent,
    SignupComponent,

    FilesComponent,
    NavbarComponent,
    ProfileComponent,
    UploadComponent,
    SampleGridApplicationComponent,
    SampleResultButtonComponent,
    AssetGridApplicationComponent,
    ResultComponent,
    ResetPasswordComponent,
    AssetComponentDetailComponent
  ],
  bootstrap: [ AppComponent ],
  entryComponents: [AssetComponentDetailComponent]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

APP-routing.module.ts

    @Injectable()
export class NoAuthGuard implements CanActivate {
  constructor(private router: Router) {}

  canActivate() {
    const activeUser = Kinvey.User.getActiveUser();
    if (activeUser) {
      return true;
    }

    // Navigate to the …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular-cli angular

9
推荐指数
2
解决办法
2923
查看次数