EmberJS 发送主操作组件
2018-01-03 19:07 更新
描述
在主操作中,组件发送一种操作。例如,当您单击按钮时,它会向组件发送操作,这称为主操作。
语句
{{my-button action="actions"}}
例子
<!DOCTYPE html> <html> <head> <title>Emberjs Sending a Primary Action</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="index"> <p>{{my-comp title=title action="compFunc"}}</p> </script> <script type="text/x-handlebars" data-template-name="components/my-comp"> <h2>Click button for primary action</h2> <input type="button" value="Click Me" {{action "compFunc"}} /><br/> <p><b>{{title}}</b></p> </script> <script type="text/javascript"> App = Ember.Application.create(); App.MyCompComponent = Ember.Component.extend({ //defining the action hook to set the title property value actions: { compFunc: function() { this.set('title', " Say Hello To Tutorialspoint..."); //This method sends the specified action when the component is used in a template this.sendAction(); } } }); </script> </body> </html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上面的代码保存在comp_prmry_act.html文件中
在浏览器中打开此HTML文件。
以上内容是否对您有帮助:
更多建议: