【问题标题】:Dynamically determine existence of template only Ember glimmer component?动态确定仅模板 Ember glimmer 组件的存在?
【发布时间】:2021-11-17 10:48:13
【问题描述】:

我知道要在 Ember 上动态确定组件的存在,我们可以使用 herehere 解释的解决方案。

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

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

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

【问题讨论】:

  • 你用的是什么 ember 源码版本?
  • @NullVoxPopuli 3.26.2

标签: javascript ember.js glimmer.js


【解决方案1】:

我在 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,它是一样的。

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

【讨论】:

  • 感谢演示。在我们的案例中,template-only.hbs 位于 templates/components 文件夹下。难道这就是它不被识别的原因吗?
  • 是的,正是——这需要移动到新位置。如果你有很多这样的工具,有一个工具可以帮助你:github.com/ember-codemods/…
猜你喜欢
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多