首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ember Octane中将"action“从模板传递到它的子组件

如何在Ember Octane中将"action“从模板传递到它的子组件
EN

Stack Overflow用户
提问于 2020-01-14 11:28:50
回答 1查看 306关注 0票数 2

我试图将一个"action“从控制器传递给当前模板的孙子组件。但它在somereason上失败了。谁能让我知道我错过了什么。

代码语言:javascript
复制
MainTemplate's Router Controller

export default class MainTemplateController extends Controller {

  field = "userId";

  @action
  save () {
    //save data
  }

}

MainTemplate.hbs

<ChildComponent @field={{this.field}} @save={{this.save}} /> 


ChildComponent.hbs 

<GrandChildComponent @field={{this.field}} @save={{this.save}} />


GrandChildComponent.hbs

<button @onClick={{action "doSomething" (readonly @field)}}>Save</button>

export default class GrandChildComponent extends Component {    
    @action
    doSomething(fieldName) {
        console.log(fieldName); // logs as "userId"
        console.log(this.args.save) // undefined
    }    
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-14 13:07:27

您的代码看起来很好。在您的ChildComponent.hbs文件中有一个小的@参数问题。

因为您是通过ChildComponent将参数从MainTemplate (savefield)传递到GrandChildComponentGrandChildComponent调用应该是这样的,

代码语言:javascript
复制
<!-- ChildComponent.hbs  -->

<GrandChildComponent @field={{@field}} @save={{@save}} />

因为这两个属性是ChildComponent组件的参数,它并不拥有它们。希望这解决了你的问题,这个cheat sheet帮助我更好地理解了辛烷:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59727160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档