简体   繁体   English

从余烬组件生命周期挂钩中,如何确定该组件是否以块形式被调用?

[英]From within an ember component life cycle hook, how can I determine if the component is being invoked in block form?

Per the Ember guides , you can determine if a component is being rendered in block format from within the template using the hasBlock keyword: 根据Ember指南 ,您可以使用hasBlock关键字确定是否从模板中以块格式呈现组件:

// foo-component.hbs
{{#if hasBlock}}
block mode
{{else}}
something else
{{/if}}

How do I do this from within foo-component.js ? 我该如何在foo-component.js执行此操作?

import Ember from 'ember';

 export default Ember.Component.extend({
         didInitAttrs: function() {
                 // hasBlock is not defined here
                 if ( this.get('hasBlock') ) {

                 }
         }
 });

You could check for template property of Component, but such behavior is deprecated: 您可以检查Component的template属性,但是不建议使用这种行为:

if (this.get('template')) {
   console.log('Component has block!');
}

Working demo. 工作演示。

根据sbatson的评论,不支持在模板中使用hasBlock

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

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