简体   繁体   English

带有 webcomponents 的 Angular 香蕉盒装 (LitElement)

[英]Angular banana-in-a-box with webcomponents (LitElement)

I'm using LitElement webcomponents with an angular application我正在使用带有角度应用程序的LitElement webcomponents

I implemented two ways data binding with a CustomEvent dispatch but I would like to use angular's banana-in-a-box syntax sugar.我使用 CustomEvent 调度实现了两种数据绑定方式,但我想使用 angular 的香蕉盒装语法糖。

Example : this stackblitz : a webcomponent with an integer property that could be incremented within the webcomponent itself示例:这个 stackblitz :一个具有整数属性的 webcomponent 可以在 webcomponent 本身内递增

Current syntax :当前语法:

<hello-world [counter]="counter" (counterChange)="counter = $event.detail.value"></hello-world>

Syntax I want to achieve :我想实现的语法:

<hello-world [(counter)]="counter"></hello-world>

When I increment from within the webcomponent the event is caught by angular because the name of the event corresponds to the name of a property + Change ( counterChange ) but the whole CustomEvent is assigned to the value instead of the $event.detail.value value当我从 webcomponent 中递增时,事件被 angular 捕获,因为事件的名称对应于属性的名称 + Change ( counterChange ) 但整个 CustomEvent 被分配给该值而不是$event.detail.value

From what I read in the documentation, angular achieves this with the EventEmitter model, which according to the code are rxjs Subjects, I'll try to go this route instead of the CustomEvent route.从我在文档中读到的内容来看,angular 使用 EventEmitter 模型实现了这一点,根据代码是 rxjs Subjects,我将尝试走这条路线而不是 CustomEvent 路线。

If anybody has any clue on how to achieve this I'd love some tips :)如果有人对如何实现这一点有任何线索,我会喜欢一些提示:)

Thanks !谢谢 !

In the webcomponent hello-word use在 webcomponent hello-word 中使用

in html component use button with:在 html 组件中使用按钮:

button --> (counter)="handleCounter($event)"

@Output() counter = new EventEmitter<number>();

// .ts
handleCounter(ev){
    ev.stopPropagation();
    ev.preventDefault();
    //Here you need to increment and set value  
    this.counter.emit(value);
  }

and use the component:并使用该组件:

<hello-world (counter)="onValueCounter($event)"></hello-world>

//.ts

  @Output() onCounter = new EventEmitter<any>();

  onValueCounter(term){
    this.onCounter .emit(term);
    console.log(term);
  }

I hope this help you!!!我希望这对你有帮助!!!

Might be that you could use some part of my solution.可能是您可以使用我的解决方案的某些部分。 I was struggling with the same issue with my Stencil project (angular-output-target), only difference was toggling a boolean for the visibility of a modal.我在我的 Stencil 项目(角度输出目标)中遇到了同样的问题,唯一的区别是切换一个布尔值以获得模态的可见性。

I went with a solution where i created a new directive with the component library tag as the selector.我采用了一个解决方案,在该解决方案中,我创建了一个新指令,并将组件库标记作为选择器。 Then applied hostlistners to the event(s) and emitted out the new value with the Angular EventEmitter.然后将主机监听器应用于事件并使用 Angular EventEmitter 发出新值。

import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';

@Directive({
  // eslint-disable-next-line @angular-eslint/directive-selector
  selector: 'library-modal',
})
export class ModalDirective {
  @Input() open: boolean = false;
  @Output() openChange: EventEmitter<boolean> = new EventEmitter();

  @HostListener('afterOpen', ['$event.detail'])
  @HostListener('afterClose', ['$event.detail'])
  updateOpen(event) {
    this.openChange.emit(event);
  }
}

I was then able to use the component with CustomEvent and two-way binding after importing the new directive to the correct module.然后,在将新指令导入到正确的模块后,我能够使用带有 CustomEvent 和双向绑定的组件。

<library-modal [(open)]="openModalBoolean">
  <p>Content</p>
</library-modal>

Could be too late for your project, but I hope it can help others :)您的项目可能为时已晚,但我希望它可以帮助其他人:)

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

相关问题 出现 Angular Migrate TSLint to ESLint Banana-In-Box 错误 - Having Angular Migrate TSLint to ESLint Banana-In-Box Error Angular 与香蕉盒语法的双向数据绑定不起作用 - Angular two way data binding with banana box syntax not working 如何使用自定义元素实现 Angular 的“盒子里的香蕉”双向绑定? - How do I implement Angular's “banana in a box” two-way binding with Custom Elements? Angular Elements WebComponents 外部 DOM 变化 - Angular Elements WebComponents external DOM Changes 如何在Angular2中向WebComponents添加样式 - How to add style to WebComponents in Angular2 通过 NestJS 后端单独服务(Angular)WebComponents - Serving (Angular) WebComponents individually over NestJS backend 最新的 Canary,Angular/Webcomponents/Polymer/Polyfill 坏了 - Latest Canary, Angular/Webcomponents/Polymer/Polyfill is broken 避免在priming列表框组件中的“ Banana In A Box”中 - Avoid Banana In A Box in primeng listbox component Angular11 + webcomponents/polyfills + IE11 == nojoy - Angular11 + webcomponents/polyfills + IE11 == nojoy Polymer LitElement&Angular - 渲染永不调用,不显示任何内容 - Polymer LitElement & Angular - render never called, no content displayed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM