Angular 7 - 为动态创建的组件添加拖放行为
创始人
2024-10-16 16:01:02
0

下面是一个示例代码,演示如何在Angular 7中为动态创建的组件添加拖放行为。

  1. 首先,确保已安装了@angular/cdk@angular/material依赖项。可以使用以下命令进行安装:
npm install @angular/cdk @angular/material
  1. 创建一个动态组件,例如DynamicComponent。可以使用ng generate component dynamic-component命令生成该组件。

dynamic-component.component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-dynamic-component',
  templateUrl: './dynamic-component.component.html',
  styleUrls: ['./dynamic-component.component.css']
})
export class DynamicComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

dynamic-component.component.html:

Dynamic Component

  1. 在主组件中创建一个容器来放置动态组件。在本例中,我们使用
    作为容器。

app.component.html:

  1. 在主组件中创建一个方法来处理拖放事件。

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 {
  components = [1, 2, 3, 4, 5];

  drop(event: CdkDragDrop) {
    moveItemInArray(this.components, event.previousIndex, event.currentIndex);
  }
}
  1. 在app.module.ts中导入DragDropModule并将其添加到imports数组中。

app.module.ts:

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

@NgModule({
  declarations: [
    AppComponent,
    DynamicComponent
  ],
  imports: [
    BrowserModule,
    DragDropModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

现在,当您运行应用程序时,您将能够在Angular 7中为动态创建的组件添加拖放行为。

相关内容

热门资讯

第二分钟辅助!wepokerp... 第二分钟辅助!wepokerplus脚本,hhpoker德州牛仔视频,机巧教程(有挂技术)1、全新机...
四分钟辅助!HH平台挂,pok... 四分钟辅助!HH平台挂,pokemmo修改器手机版,攻略教程(有挂存在)1、pokemmo修改器手机...
第2分钟辅助!wepoker底... 第2分钟辅助!wepoker底牌透视,aapoker怎么开辅助器,演示教程(有挂解惑)1、玩家可以在...
第2分钟辅助!wpk透视怎么安... 第2分钟辅助!wpk透视怎么安装,wepoker手机助手,手段教程(有挂教学)1、wpk透视怎么安装...
七分钟辅助!wepoker免费... 您好,wepoker免费脚本弱密码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
第6分钟辅助!wejoker开... 第6分钟辅助!wejoker开挂,aa poker辅助,妙计教程(有挂细节)1、aa poker辅助...
6分钟辅助!德州透视脚本,hh... 6分钟辅助!德州透视脚本,hh poker软件,教程书教程(真的有挂)1、任何德州透视脚本透视是真的...
十分钟辅助!pokemmo手机... 十分钟辅助!pokemmo手机版脚本免费,wepoker祈福有用吗,手段教程(有挂方式)1、poke...
第2分钟辅助!wepoker免... 第2分钟辅助!wepoker免费脚本咨询,wepoker透视器免费,积累教程(有挂总结)wepoke...
第六分钟辅助!aapoker怎... 第六分钟辅助!aapoker怎么设置提高好牌几率,德普之星透视免费,法子教程(有挂猫腻)所有人都在同...