angular2 – ngOnInit在可注入类被实例化时不被调用

前端之家收集整理的这篇文章主要介绍了angular2 – ngOnInit在可注入类被实例化时不被调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么在解析Injectable类时调用ngOnInit()?

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()');
    }

}

控制台输出

FROM constructor()
Lifecycle hooks,像OnInit()与指令和组件一起工作。他们不与其他类型,如在您的情况下的服务工作。来自文档:

A Component has a lifecycle managed by Angular itself. Angular creates it,renders it,creates and renders its children,checks it when its data-bound properties change,and destroys it before removing it from the DOM.

Directive and component instances have a lifecycle as Angular creates,updates,and destroys them.

原文链接:https://www.f2er.com/angularjs/146066.html

猜你在找的Angularjs相关文章