Call a method of the child component

前端 未结 4 2013
执笔经年
执笔经年 2020-12-07 17:42

I have a nested child component like this:


    

         


        
4条回答
  •  眼角桃花
    2020-12-07 18:07

    Parent and child can communicate via data binding.

    Example:

    @Component({
        selector: 'child-component',
        inputs: ['bar'],
        template: `"{{ bar }}" in child, counter  {{ n }}`
    })
    class ChildComponent{
        constructor () {
            this.n = 0;
        }
        inc () {
            this.n++;
        }
    }
    
    @Component({
        selector: 'my-app',
        template: `
            
    `, directives: [ChildComponent] }) class AppComponent { constructor () { this.bar = 'parent var'; } } bootstrap(AppComponent);

    Demo

    #f creates a reference to the child component and can be used in template or passed to function. Data from parent can be passed by [ ] binding.

提交回复
热议问题