简体   繁体   English

Angular - 无法获取父组件数据

[英]Angular - Cannot get parent component data

I'm passing a function as parameter from parent to child component.我正在将一个函数作为参数从父组件传递给子组件。 When click event is occurred, function of parent component trigger, but all the property of parent component is undefined.当点击事件发生时,会触发父组件的功能,但父组件的所有属性都是未定义的。 For example,例如,

Parent Component父组件

export class AppComponent implements OnInit {
    constructor( private notificationService: NotificationService ) {}

    unreadNotification(): Observable<any> {
        // here this.notificationService is undefined
        console.log( this.notificationService );
    }
}

Parent html父 html

<notification-menu [unread]= "unreadNotification"></notification-menu>

child Component子组件

export class NotificationMenuComponent implements OnInit {
    @Input() updateUnread: Function;
}

child html子HTML

<button type="button" class="icon-button" (click)="updateUnread()">
</button>

Now when I click on notification button, unreadNotification is triggered, but value of this.notificationService in console.log is undefined .现在,当我单击通知按钮时,会触发unreadNotification ,但console.logthis.notificationService值是undefined

How can I solve this?我该如何解决这个问题?

You should use @Input() to pass values from parent to child and @Output() to pass values from child to parent .您应该使用@Input()将值从parent传递到child@Output()将值从child传递到parent

Child HTML:子 HTML:

<button type="button" class="icon-button" (click)="update()">
</button>

Child Component:子组件:

export class NotificationMenuComponent implements OnInit {
    @Output() updateUnread = new EventEmitter<string>();

    update() {
        this.updateUnread.emit("I am working man!");
    }
}

Parent HTML:父 HTML:

<notification-menu (updateUnread)= "unreadNotification($event)"></notification-menu>

Parent Component:父组件:

export class AppComponent implements OnInit {
    constructor( private notificationService: NotificationService ) {}

    unreadNotification(dataFromChild: string) {
        console.log(dataFromChild);
    }
}

The answer from @nimeresam is good advice - using an @Output is an idomatic way to achieve this. @nimeresam 的答案是很好的建议 - 使用@Output是实现这一目标的@Output方法。

It's worth noting though, that the reason that your original solution doesn't work is due to the way that javascript handles the this context.不过值得注意的是,您的原始解决方案不起作用的原因是 javascript 处理this上下文的方式。

Writing (click)="updateUnread()" is equivalent to saying this.updateUnread() with this being NotificationMenuComponent - as notificationService does not exist on NotificationMenuComponent you get the undefined error.(click)="updateUnread()"相当于说this.updateUnread()并且 this 是NotificationMenuComponent - 因为 NotificationMenuComponent 上不存在 notificationService 你会得到未定义的错误。

To have the context of the parent component used, you would need to bind the context to the updateUnread function before passing it into the child component.要使用父组件的上下文,您需要updateUnread上下文绑定到updateUnread函数,然后再将其传递给子组件。

This can be achieved either by converting the function to be an arrow functionn, or using Function.bind这可以通过将函数转换为箭头函数或使用Function.bind

See:看:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

It's normally a good idea to enable the typescript option for --noImplicitThis to help catch these errors (though unsure if it will detect it in this case)--noImplicitThis启用--noImplicitThis选项以帮助捕获这些错误通常是个好主意(尽管不确定在这种情况下是否会检测到它)

You can use arrow function so that you can use parent component's information.您可以使用arrow功能,以便您可以使用父组件的信息。 You can try as like as given below.您可以尝试如下所示。

updateUnreadNotification = () => {
   // by using arrow function you can get notificationService information
   console.log( this.notificationService );
}

Hope your problem will be solve by this.希望您的问题将由此得到解决。

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

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