简体   繁体   English

Angular web 组件(元素)输入在 ngOnInit 中未定义

[英]Angular web component (elements) input is undefined in ngOnInit

I am building a web component in Angular.我正在 Angular 中构建一个 web 组件。 My objective is to evaluate if the consumer of the web component provided a given input, in this case "accessToken".我的目标是评估 web 组件的使用者是否提供了给定的输入,在本例中为“accessToken”。 However the input is undefined in ngOnInit() , but prints in ngOnChanges()但是输入在ngOnInit()中未定义,但在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?为什么它在ngOnInit生命周期钩子中不可用,我如何确定用户是否以最有效的方式提供了输入?

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 .我不敢相信这回答了这个问题,但显然我只需要更改我的输入accessToken ==> access_token

I find out by using the Chrome dev tools, and inspecting the component(!)我通过使用 Chrome 开发工具并检查组件(!)

在此处输入图像描述

This is also explained here :这也在这里解释:

You should always use lowercase characters.您应该始终使用小写字符。

One of those days - but it works now.那些日子之一 - 但它现在有效。 Also in ngOnInit()同样在ngOnInit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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