Vue实现右键菜单(vue-contextmenu模块)

Vue实现右键菜单(vue-contextmenu模块)

一、关于右键点击事件

1:右键时,不阻止浏览器行为

<div style="width: 100px;height: 100px;background-color: #5daf34;float: left"></div>

在这里插入图片描述
2:右键时,阻止浏览器行为

<div @contextmenu style="width: 100px;height: 100px;background-color: blueviolet;float: left"></div>

在这里插入图片描述
3:右键时,阻止浏览器行为,并调用相关方法

<div @contextmenu.prevent="show" style="width: 100px;height: 100px;background-color: lightcoral;float: left"></div>
methods: {
  show() {
    this.$message({type: "info", message: "右键点击事件"})
  }
}

在这里插入图片描述

二、实现右键菜单

1.引入vue-contextmenu模块

npm install vue-contextmenu --save

在这里插入图片描述

2.在main.js中引入该模块

// 右键点击事件(右键生成菜单)
import VueContextMenu from 'vue-contextmenu'
Vue.use(VueContextMenu);

在这里插入图片描述

3.相关代码

1:template标签

<div style="background-color: #66b1ff;width: 700px;height: 100px;margin-top: 70px">
    右键弹出菜单
    <el-table :data="tableData" style="width: 100%">
       <el-table-column prop="id" label="编号" width="90">
          <template slot-scope="scope">
            <div @contextmenu.prevent="contextMenu($event,scope.row.id)">{{scope.row.id}}</div>
          </template>
       </el-table-column>
       <el-table-column prop="date" label="日期" width="180">
          <template slot-scope="scope">
            <div @contextmenu.prevent="contextMenu($event,scope.row.id)">{{scope.row.date}}</div>
          </template>
       </el-table-column>
       <el-table-column prop="name" label="姓名" width="180">
          <template slot-scope="scope">
            <div @contextmenu.prevent="contextMenu($event,scope.row.id)">{{scope.row.name}}</div>
          </template>
       </el-table-column>
       <el-table-column prop="address" label="地址">
          <template slot-scope="scope">
            <div @contextmenu.prevent="contextMenu($event,scope.row.id)">{{scope.row.address}}</div>
          </template>
       </el-table-column>
   </el-table>
</div>
<vue-context-menu :contextMenuData="contextMenuData" @insertData="insertData" @deleteData="deleteData"
                        @saveData="saveData" @downloadData="downloadData"></vue-context-menu>

2:script标签

data() {
   return {
      contextMenuData: {

        // 菜单名称
        menuName: "rightMenu",

        // 菜单显示的坐标位置
        axis: {
          x: null,
          y: null
        },
        // 菜单选项
        menulists: [
        {
           // 绑定事件
           fnHandler: "insertData",

           // icon图标
           icoName: "el-icon-plus",

           // 菜单选项名称
           btnName: "新增数据"
         },
         {
           // 绑定事件
           fnHandler: "deleteData",

           // icon图标
           icoName: "el-icon-close",

           // 菜单选项名称
           btnName: "删除数据"
         },
         {
           // 绑定事件
           fnHandler: "saveData",

           // icon图标
           icoName: "el-icon-check",

           // 菜单选项名称
           btnName: "保存数据"
         },
         {
           // 绑定事件
           fnHandler: "downloadData",

           // icon图标
           icoName: "el-icon-download",

           // 菜单选项名称
           btnName: "下载数据"
         }
       ]
     },
     tableData: [{
       id: '001',
       date: '2016-05-02',
       name: '王小虎',
       address: '上海市普陀区金沙江路 1518 弄'
     }, {
       id: '002',
       date: '2016-05-04',
       name: '王小虎',
       address: '上海市普陀区金沙江路 1517 弄'
     }, {
       id: '003',
       date: '2016-05-01',
       name: '王小虎',
       address: '上海市普陀区金沙江路 1519 弄'
     }, {
       id: '004',
       date: '2016-05-03',
       name: '王小虎',
       address: '上海市普陀区金沙江路 1516 弄'
     }]
   }
}
methods: {
   insertData() {
     // 新增数据
     this.$message({type: "info", message: "新增数据"});
   },
   deleteData() {
      // 删除数据
      this.$message({type: "info", message: "删除数据"});
   },
   saveData() {
      // 保存数据
      this.$message({type: "info", message: "保存数据"})
   },
   downloadData() {
      // 下载数据
      this.$message({type: "info", message: "下载数据"})
   },
   contextMenu(e, data) {
      // 右键单击事件(修改菜单显示坐标)
      let x = e.clientX;
      let y = e.clientY;
      // Get the current location
      this.contextMenuData.axis = {x, y};
      this.$message({type: "info", message: "编号:" + data});
   }
}

3:style标签

<style scoped>
  .vue-contextmenuName-rightMenu {
    background-color: #66b1ff !important;
  }

  /deep/ .vue-contextmenu-listWrapper .context-menu-list[data-v-1931f86e] {
    margin-left: -37px;
  }
</style>

4.开始测试

在这里插入图片描述


总结

每天一个提升小技巧!!!

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小辰哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值