how to access Angular2 component specific data in console?

后端 未结 4 1334
萌比男神i
萌比男神i 2020-11-30 02:54

Is there any way to access Angular2 specific component specific data in console, for debugging purpose?

Like Angular1 has capability to access its components value i

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 03:42

    Using global scope variable.(any browser)

    index.html

    
    

    on your component file

    declare var test: any;
    

    After ngOnInit() of that component file

    For example

    ngOnInit() {
       test = this;
    
    }
    

    Now we can access every variables and function of that component including services(imported on that component).

    If you want to prevent accessing test for let's say production, you can conditionally give access to test like:

    constructor(private _configService: ConfigService) {
    }
    ngOnInit() {
       if(_configService.prod) {
          test = this;
       }
    
    }
    

    Here _configService means the instance of service used by all components and services.
    So variable would be set like:

    export class ConfigService {
       prod = false;
    }
    

提交回复
热议问题