加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:45:22 所属栏目:安全 来源:网络整理
导读:为什么在解析Injectable类时调用ngOnInit()? 码 import {Injectable,OnInit} from 'angular2/core';import { RestApiService,RestRequest } from './rest-api.service';@Injectable()export class MovieDbService implements OnInit { constructor(private
为什么在解析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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读