类型的参数不能分配给类型的参数' cdkdragmove< any gt;'

发布于 2025-01-17 18:29:42 字数 986 浏览 1 评论 0原文

我试图获取可拖动元素移动时的位置,但找不到任何与 CdkDragMove 配合使用的属性。

HTML 模板

<div
  cdkDropList
  [cdkDropListData]="day.lunch"
  [cdkDropListConnectedTo]="dropTargetIds"
  class="example-list"
  (cdkDropListDropped)="drop($event)"
  (cdkDragMoved)="dragMoved($event)" // <--- Error (see below)
  >
    //Actual content
  </div>

TypeScript

dragMoved(event:CdkDragMove<any>){
    // get PosX & PosY
  }
  drop(event: CdkDragDrop<Recipe[]>) {
    // other stuff
  }

我收到此错误:

“Event”类型的参数不可分配给类型的参数 'CdkDragMove'。类型“事件”缺少以下属性 来自类型“CdkDragMove”:源、pointerPosition、事件、 距离,三角洲

我也尝试过使用 DragMoved 而不是 $event 但后来我得到了这个

类型参数'(event: CdkDragMove) => void' 不可赋值 到“CdkDragMove”类型的参数。输入 '(事件: CdkDragMove) => void' 缺少以下属性 输入“CdkDragMove”:源、指针位置、事件、距离、 德尔塔

我无法理解这个问题,因为该事件正在处理 drop 函数。 有什么想法吗?

I'm trying to get the position of a draggable element as it's moved around, but I can't find any attribute that works with CdkDragMove.

HTML Template

<div
  cdkDropList
  [cdkDropListData]="day.lunch"
  [cdkDropListConnectedTo]="dropTargetIds"
  class="example-list"
  (cdkDropListDropped)="drop($event)"
  (cdkDragMoved)="dragMoved($event)" // <--- Error (see below)
  >
    //Actual content
  </div>

TypeScript

dragMoved(event:CdkDragMove<any>){
    // get PosX & PosY
  }
  drop(event: CdkDragDrop<Recipe[]>) {
    // other stuff
  }

I get this error:

Argument of type 'Event' is not assignable to parameter of type
'CdkDragMove'. Type 'Event' is missing the following properties
from type 'CdkDragMove': source, pointerPosition, event,
distance, deltangtsc

I also tried with dragMoved instead of $event but then I get this

Argument of type '(event: CdkDragMove) => void' is not assignable
to parameter of type 'CdkDragMove'. Type '(event:
CdkDragMove) => void' is missing the following properties from
type 'CdkDragMove': source, pointerPosition, event, distance,
delta

I can't get my head around this, 'cause the event is working on the drop function instead.
Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2025-01-24 18:29:42

当我无法将 DragDropModule 导入到我的模块中时,出现此错误。

import { DragDropModule } from '@angular/cdk/drag-drop';

I got this error when I had failed to import the DragDropModule into my module.

import { DragDropModule } from '@angular/cdk/drag-drop';
冬天的雪花 2025-01-24 18:29:42

解决

我发现(cdkdragmoved)=“ dragmaved($ event)”必须放置在cdkdrag element上,而不是在CDKDROPLIST元素上。

<div
            cdkDropList
            [cdkDropListData]="day.lunch"
            [cdkDropListConnectedTo]="dropTargetIds"
            class="example-list"
            (cdkDropListDropped)="drop($event)"
            // It was here
          >
            <mat-chip-list class="mat-chip-list-stacked" >
              <mat-chip cdkDrag (cdkDragMoved)="dragMoved($event)"> //<--- Must be Here
                // content
              </mat-chip>
            </mat-chip-list>
          </div>

Solved

I found out that the (cdkDragMoved)="dragMoved($event)" binding had to be placed at the cdkDrag element, and not at the cdkDropList element.

<div
            cdkDropList
            [cdkDropListData]="day.lunch"
            [cdkDropListConnectedTo]="dropTargetIds"
            class="example-list"
            (cdkDropListDropped)="drop($event)"
            // It was here
          >
            <mat-chip-list class="mat-chip-list-stacked" >
              <mat-chip cdkDrag (cdkDragMoved)="dragMoved($event)"> //<--- Must be Here
                // content
              </mat-chip>
            </mat-chip-list>
          </div>
回梦 2025-01-24 18:29:42

正如错误消息所示,您需要添加一个类型来告诉 cdkDragMoved 正在移动什么。就像您已经对 drop 所做的那样:

dragMoved(event:CdkDragMove<Recipe[]>){
   // get PosX & PosY
}

请参阅文档: https://material.angular.io /cdk/drag-drop/api

As the error message says you need to add a Type to tell the cdkDragMoved what is being moved. Like you already did for drop:

dragMoved(event:CdkDragMove<Recipe[]>){
   // get PosX & PosY
}

See the Documentation: https://material.angular.io/cdk/drag-drop/api

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文