# ContextMenu 右键菜单

# 使用

import Vue from "vue";
import VueWidget from "@vensst/vue-widget";
Vue.use(VueWidget.ContextMenu);
1
2
3
区域一
区域二
区域三
区域四,添加列表,在列表上右击
    <template>
    <div class="container">
      <div class="item item1">
        <ven-context-menu
            :menus="menus"
            :extend="{ data: 1 }"
            @command="handleCommand"
            style="width: 100%; height: 100%"
        >
          <div>区域一</div>
        </ven-context-menu>
      </div>
      <div class="item item2">
        <VenContextMenu
            :menus="menus"
            :extend="{ data: 1 }"
            @command="handleCommand"
            style="width: 100%; height: 100%"
        >
          <div>区域二</div>
          <VenContextMenu
              :menus="menus2"
              :extend="{ data: 2 }"
              @command="handleCommand"
              style="width: 200px; height: 200px"
          >
            <div class="box">区域三</div>
          </VenContextMenu>
        </VenContextMenu>
      </div>
      <div class="item item3">
        区域四,添加列表,在列表上右击
        <div class="todo">
          <div>
            <input v-model="todoName" type="text" />
            <button @click="addTodo">添加</button>
          </div>
          <ul class="list">
            <template v-for="(item, index) in todoList">
              <VenContextMenu
                  :menus="menus"
                  :key="index"
                  :extend="{ data: item }"
                  @command="handleCommand"
              >
                <li>{{ item }}</li>
                <template v-slot:menu="{ menu, data }">
                  <div>{{ data }}:{{ menu.name }}</div>
                </template>
              </VenContextMenu>
            </template>
          </ul>
        </div>
      </div>
    </div>
    </template>
    
    <script>
    // import ContextMenu from "../components/ContextMenu.vue";
    
    export default {
    mixins: [],
    props: {},
    data() {
      return {
        menus: [
          {
            name: "新增",
            label: "add",
            callback: this.addUser,
          },
          {
            name: "编辑",
            label: "edit",
            callback: this.editUser,
          },
          {
            name: "删除",
            label: "delete",
            callback: this.deleteUser,
          },
        ],
        menus2: [
          {
            name: "新增2",
            label: "add",
          },
          {
            name: "编辑2",
            label: "edit",
          },
          {
            name: "删除2",
            label: "delete",
          },
        ],
        todoName: "",
        todoList: [],
      };
    },
    mounted() {},
    methods: {
      addUser() {
        console.log("新增");
      },
      editUser() {
        console.log("编辑");
      },
      deleteUser() {
        console.log("删除");
      },
      handleCommand(menu) {
        console.log(menu);
      },
      addTodo() {
        this.todoList.push(this.todoName);
        this.todoName = "";
      },
    },
    };
    </script>
    
    <style>
    .container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: 300px 300px;
    column-gap: 20px;
    row-gap: 20px;
    }
    .item3 {
    //grid-row: 1 / 3; /* 合并占据 1到2 行,不包括3 */
    grid-column: 1 / 3; /* 合并占据 1到1 列,不包括2 */
    }
    
    .item1 {
    background-color: #ddd;
    }
    .item2 {
    background-color: skyblue;
    }
    .item3 {
    //background-color: yellow;
    }
    
    .item {
    border: 1px solid #ddd;
    }
    
    .box {
    width: 200px;
    height: 200px;
    background-color: yellow;
    }
    
    .todo li{
    line-height: 32px;
    border: 1px solid #ddd;
    width: 300px;
    //margin-bottom: 20px;
    }
    </style>
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    显示代码 复制代码

    # 属性

    参数 说明 类型 可选值 默认值
    menus 菜单列表 Array -- --
    extend 扩展数据 Object -- --

    # 事件

    事件名称 说明 回调参数
    command 点击菜单项触发的事件回调 (value:Object)

    # 插槽

    name 说明
    menu 自定义菜单项