简体   繁体   English

emberjs:微光 web 组件传递数组/对象不起作用

[英]emberjs: Glimmer web component pass array/objects not working

One of our business problem statement is, we have to create component such that any consumer can use them as a widget and embed in their website.我们的业务问题陈述之一是,我们必须创建组件,以便任何消费者都可以将它们用作小部件并嵌入到他们的网站中。 And the thing is we already have those component made with ember.问题是我们已经有了用 ember 制作的那些组件。

We're not aware that which stack the consumer website going to have, that could be random.我们不知道消费者网站将拥有哪个堆栈,这可能是随机的。 So we thought of converting these ember components to web components.所以我们想到了将这些 ember 组件转换为 web 组件。

We did small POC where we were not able to create web component out of ember component using glimmer.我们做了小型 POC,我们无法使用 glimmer 从 ember 组件中创建 web 组件。 But we're facing couple of problems但是我们面临着几个问题

  1. We're not able to pass objects/arrays to web components with glimmer (we tried using pass it through properties)我们无法使用微光将对象/数组传递给 web 组件(我们尝试使用通过属性传递它)
  2. Somehow shadow DOM is not working when web component gets rendered当 web 组件被渲染时,影子 DOM 不工作

For using simple glimmer component I have followed https://glimmerjs.com/guides/using-glimmer-as-web-components为了使用简单的微光组件,我遵循了 https://glimmerjs.com/guides/using-glimmer-as-web-components

Gist:要旨:

Idea主意

The idea was to create framework agnostic component, so that it can be used inside different applications flawlessly.这个想法是创建与框架无关的组件,以便它可以完美地在不同的应用程序中使用。 The component was already written in emberjs.该组件已经用 emberjs 编写。

Solution解决方案

Solution is to write web-component for obvious, but we've our code already written in emberjs add-on.显而易见的解决方案是编写 web-component,但我们的代码已经用 emberjs 插件编写。 So we decided to migrate add-on project to latest ember, after we migrated add-on components to glimmerjs and converted that glimmerjs component to web-component (using @glimmer/web-component ) and use it across.因此,我们决定将附加项目迁移到最新的 ember,在我们将附加组件迁移到 glimmerjs 并将 glimmerjs 组件转换为 web-component (使用@glimmer/web-component )并使用它之后。

We took an effort, and made it working.我们付出了努力,并使其发挥了作用。 I'm sharing the current solution that we've applied.我正在分享我们应用的当前解决方案。

As we were facing couple of challenges as I mentioned in my question.正如我在问题中提到的那样,我们面临着几个挑战。 I'll answer those points one by one.我会一一回答这些问题。

  1. For passing object between two component, we're passing it by raising CustomEvent from child-component to parent-component为了在两个组件之间传递 object,我们通过将CustomEventchild-component提升到parent-component组件来传递它

    didInsertElement() { this.element.dispatchEvent( new CustomEvent('data-ready', { bubbles: true, detail: this }) ); }
  2. After a research found that, glimmer does not support shadow DOM, they don't have any plan for supporting it yet.经过研究发现,glimmer 不支持 shadow DOM,他们还没有支持它的计划。 And re-analysing the requirement once we thought shadow-dom is good to have feature.一旦我们认为shadow-dom具有特征是好的,重新分析需求。 We're using CSS specificity (traditional way) to isolate CSS specific to component:p我们正在使用 CSS 特异性(传统方式)来隔离特定于组件的 CSS:p

Though after applying above things we're in good shape.尽管在应用了上述内容之后,我们的状态良好。 If in case you wanted to have a look at the code.如果您想查看代码。 Please check this github repository (initial look)请检查此github 存储库(初步查看)

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

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