EmberJS - 与包装内容共享组件数据

描述

您可以将组件数据与其包装内容共享。假设我们有一个名为 {{my-component}} 的组件,我们可以为其提供 style 属性来编写帖子。您可以这样写 −

{{#my-component editStyle="markdown-style"}}

组件带有哈希值并提供给模板。editStyle 可用作组件助手的参数。

示例

以下示例指定将组件数据与其包装内容共享。创建一个名为 post-action 的组件,该组件将在 app/components/ 下定义。

打开 post-action.js 文件并添加以下代码 −

import Ember from 'ember';

export default Ember.Component.extend({
   actions: {
      compFunc: function () {
         this.set('title', "Tutorialspoint...");
         //此方法发送指定操作
         this.sendAction();
      }
   }
});

现在使用以下代码打开组件模板文件 post-action.hbs

<input type="button" value="Click me" {{action "compFunc" bodyStyle="compact-style"}} /><br/>
//包装"title"属性值
<p><b>Title:</b> {{title}}</p>
{{yield}}

打开 index.hbs 文件并添加以下代码:

<b>Click the button to check title property value</b>
{{post-action title=title action="compFunc"}}
{{outlet}}

输出

运行 ember 服务器,您将获得以下输出 −

Ember.js 组件共享包装内容

当您单击按钮时,compFunc() 函数将触发并显示以下输出 −

Ember.js 组件共享包装内容

emberjs_component.html