javascript - cdk-drop 不是已知元素

标签 javascript angular drag-and-drop drag angular7

我正在使用 Angular 7 的新功能“拖放”,我按照官方文档上的所有步骤进行操作: https://material.angular.io/cdk/drag-drop/overview

但我收到此错误: **

Uncaught Error: Template parse errors: 'cdk-drop' is not a known element: 1. If 'cdk-drop' is an Angular component, then verify that it is part of this module. 2. If 'cdk-drop' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

**

这是我的 app.component.html 代码:

<cdk-drop cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
  <div class="example-box" *ngFor="let movie of movies" cdkDrag>{{movie}} 
  </div>
</cdk-drop>

这是我的 app.component.ts 代码:

import { Component } from '@angular/core';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    movies = [
    'Episode I - The Phantom Menace',
    'Episode II - Attack of the Clones',
    'Episode III - Revenge of the Sith',
    'Episode IV - A New Hope',
    'Episode V - The Empire Strikes Back',
    'Episode VI - Return of the Jedi',
    'Episode VII - The Force Awakens',
    'Episode VIII - The Last Jedi'
    ];

    drop(event: CdkDragDrop<string[]>) {
        moveItemInArray(this.movies, event.previousIndex, event.currentIndex);
    }
}

注意:我正在使用=> Angular:7.1.2 如果我通过 div 更改 cdk-drop html 元素也不起作用:(

最佳答案

您阅读的文档一定已过时。我关注了一篇博客文章并遇到了同样的问题。

元素<cdk-drop>已经不存在了。而是使用指令 cdkDropList。

发生奇怪错误时,请务必先检查最新的文档。

截至今天,此示例有效 https://stackblitz.com/angular/moyomqpeprev?file=app%2Fcdk-drag-drop-sorting-example.html

关于javascript - cdk-drop 不是已知元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53728742/

相关文章:

javascript - 从 html 页面访问 js 中定义的变量?

javascript - Node.js:系统发送两次 POST 请求

javascript - JQTouch - 向前导航而不将页面添加到历史记录

ckeditor - Angular2 + CKEditor - 观察变化

html - Angular Material 2 : How to put a fab button on top of the md-table at the bottom right corner?

drag-and-drop - 拖放对象引用

javascript - 类型断言不执行类型转换?

angular - 无效管道参数 : '333,00' for pipe 'DecimalPipe' ?

wpf - 无法让简单的 WPF 拖放工作

javascript - 使用 Raphael JS 拖放 SVG