繁体   English   中英

动态确定仅模板 Ember glimmer 组件的存在?

[英]Dynamically determine existence of template only Ember glimmer component?

我知道要动态确定 Ember 上某个组件的存在,我们可以使用此处此处说明的解决方案。

我们有一个使用.hasRegistration('component:${component}')的助手

只要定义了 .js 文件,这也适用于 Glimmer 组件,但不适用于仅模板的 Glimmer 组件 在这种情况下,该组件似乎没有注册。

有谁知道也适用于仅模板微光组件的解决方案?

我在 stackblitz 上为你做了一个演示:

https://stackblitz.com/edit/github-k7htnb?file=app%2Fcomponents%2Fdemo.hbs

{
  "component:template-only": true,
  "template:template-only": false
}

这是显示您正在执行的操作在 Ember 3.28 中有效的代码:

<pre>{{this.format this.results}}</pre>
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { getOwner } from '@ember/application';

export default class Demo extends Component {
  format = (data) => JSON.stringify(data, null, 2);

  @tracked name = 'template-only';

  get results() {
    let owner = getOwner(this);
    let name = this.name;

    return {
      [`component:${name}`]: owner.hasRegistration(`component:${name}`),
      // added this for curiosity
      [`template:${name}`]: owner.hasRegistration(`template:${name}`),
    };
  }
}

现在,如果我将 ember-source 更改为 3.26.1,它是一样的。

也许您正在做的事情和这个演示正在做的事情之间存在轻微的代码不匹配?

暂无
暂无

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

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