简体   繁体   中英

Angular web component (elements) input is undefined in ngOnInit

I am building a web component in Angular. My objective is to evaluate if the consumer of the web component provided a given input, in this case "accessToken". However the input is undefined in ngOnInit() , but prints in ngOnChanges()

    _accessToken;
    @Input('accessToken')
    set accessToken(accessToken: string) {
        this._accessToken = accessToken;
        console.log(accessToken); // This prints the token, but after ngOnInit??
    }

    ngOnInit() {
      console.log(this._accessToken); // Why is this undefined?
    }

    ngOnChanges(changes) {
      console.log(changes); // This prints the token??
    }

Why it's not available in the ngOnInit life-cycle hook, and how do I determine if the user provided the input or not in the most efficient way?

Any ideas are welcome

I can't believe that this answers the question, but apparently I simply had to change my input from accessToken ==> access_token .

I find out by using the Chrome dev tools, and inspecting the component(!)

在此处输入图像描述

This is also explained here :

You should always use lowercase characters.

One of those days - but it works now. Also in ngOnInit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM