简体   繁体   English

使用 ember-maybe-in-element 将组件移动到另一个 DOM 节点

[英]Move component to another DOM node using ember-maybe-in-element

I have a use case where I can move a child component to different DOM location within same page/route dynamically.我有一个用例,我可以将子组件动态移动到同一页面/路由内的不同 DOM 位置。

home.hbs主页.hbs

<div class="container">
  <div class="content">
    <!-- Place where I want to place Child -->
   </div>
</div>
<Parent></Parent>

Parent.hbs父母.hbs

<h1>This is parent component</h1>
<Child></Child>

child.hbs儿童.hbs

Hello World

child.js孩子.js

const mainContainer = document.querySelector('.container .content');
const myElm = this.element.querySelector('[data-child-content]');
mainContainer.appendChild(myElm);

I want to use ember-maybe-in-element addon instead of using appendChild .我想使用ember-maybe-in-element插件而不是使用appendChild

The in-element helper renders the block content outside of the regular flow, into a DOM element given by its destinationElement positional argument.元素内助手将常规流之外的块内容呈现到由其 destinationElement 位置参数给出的 DOM 元素中。

child.js孩子.js

get destinationElement() {
  return document.querySelector('.container .content');
}

child.hbs儿童.hbs

{{#in-element this.destinationElement}}
   <div>Hello World</div>
{{/in-element}}

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

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