【发布时间】:2019-11-05 06:58:32
【问题描述】:
我正在使用 Angular CDK 创建一个简单的 Angular 拖放应用程序。我只想拖我的 盒子并将其放到另一个盒子中。但是当释放拖放区域内的拖动框时,我得到了错误。请帮助修复它。
我遇到的错误
Unhandled Promise rejection: Failed to execute 'appendChild' on 'Node': The new child element contains the parent. ; Zone: <root> ; Task: Promise.then ; Value: DOMException: Failed to execute 'appendChild' on 'Node': The new child element contains the parent.
at DragRef._cleanupDragArtifacts (http://localhost:4200/vendor.js:1615:121)
at http://localhost:4200/vendor.js:1471:22
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
at Zone.run (http://localhost:4200/polyfills.js:3130:43)
at http://localhost:4200/polyfills.js:3861:36
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3397:31)
at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
at drainMicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)
at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:3475:21)
at invokeTask (http://localhost:4200/polyfills.js:4609:14) Error: Failed to execute 'appendChild' on 'Node': The new child element contains the parent.
at DragRef._cleanupDragArtifacts (http://localhost:4200/vendor.js:1615:121)
at http://localhost:4200/vendor.js:1471:22
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
at Zone.run (http://localhost:4200/polyfills.js:3130:43)
at http://localhost:4200/polyfills.js:3861:36
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3397:31)
at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
at drainMicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)
at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:3475:21)
at invokeTask (http://localhost:4200/polyfills.js:4609:14)
api.onUnhandledError @ zone-evergreen.js:651
handleUnhandledRejection @ zone-evergreen.js:675
api.microtaskDrainDone @ zone-evergreen.js:668
drainMicroTaskQueue @ zone-evergreen.js:566
invokeTask @ zone-evergreen.js:469
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
我会在下面粘贴我的代码
我的 app.component.html
<section>
<div class="wrapper">
<div class="outerBox">
<div class="innerBox" cdkDropList [cdkDropListConnectedTo]="[dropZone]" cdkDrag></div>
</div>
<div class="outerBox" #dropZone="cdkDropList" cdkDropList (cdkDropListDropped)="onItemDrop($event)">
</div>
</div>
</section>
和 app.component.ts
import { Component } from '@angular/core';
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
onItemDrop(event) {
console.log(event);
}
}
app.component.css 文件看起来像
.wrapper {
display: flex;
}
.outerBox {
width: 300px;
height: 500px;
border: 1px solid #333;
padding: 10px;
margin: 10px;
}
.innerBox {
width: 100px;
height: 100px;
background: skyblue;
}
请帮助解决错误。
提前致谢。
【问题讨论】:
-
您可以参考-stackoverflow.com/questions/25216460/…。希望对你有帮助
-
是的,我也检查过。它的 javascript 解决方案,但我需要修复角度拖放 cdk。
-
这肯定会有所帮助 - material.angular.io/cdk/drag-drop/overview
-
我创建了 stackblitz 来重现问题。您可以在删除蓝盒后检查控制台。 stackblitz.com/edit/angular-ebfpvj
标签: javascript angular drag-and-drop angular-material angular-cdk