分享记录vue3+ts的超简单且通俗易懂的右键菜单。

遇到个即时聊天项目,需要对对话框进行右键操作,例如撤回、复制、转发等,这里分享一个比较好用小白也能看懂的右键菜单vue3-menus库。开整!
一、安装引用
npm install vue3-menus

yarn add vue3-menus

// 全局注册组件、指令、方法
import { createApp } from 'vue';
import Menus from 'vue3-menus';
import App from './App.vue';
const app = createApp(App);
app.use(Menus);
app.mount('#app');
//或者在单个页面注册
import { defineComponent, shallowRef } from "vue";
import { menusEvent } from 'vue3-menus';

二、使用组件
//组件有三种方法可以实现,我这边用的是单页面注册
import { ref, reactive, toRefs, onMounted, defineComponent, shallowRef, nextTick } from 'vue'
import { menusEvent, directive, Vue3Menus } from 'vue3-menus';
1.自定义指令方式使用
 <div style="width: 500px;height: 500px;border:solid 1px black;" v-menus:right="menus">
    指令方式打开菜单
  </div>
const vMenus = directive
const menus = shallowRef({
  menus: [
    {
      label: "复制",
      tip: '复制改内容',
      click: () => {
        console.log('复制了');
      }
    },
    {
      label: "关闭",
      click: () => {
        console.log('关闭了');
      }
    }
  ]
});

2.事件方式打开菜单
<div style="width: 500px;height: 500px;border:solid 1px black;" @click.stop @contextmenu="rightClick">
    事件方式打开菜单
  </div>
const menus = shallowRef({
  menus: [
    {
      label: "复制",
      tip: '复制改内容',
      click: () => {
        console.log('复制了');
      }
    },
    {
      label: "关闭",
      click: () => {
        console.log('关闭了');
      }
    }
  ]
});
const rightClick = (event: any) => {
  menusEvent(event, menus.value, 0);
  event.preventDefault();
}
3.组件方式打开菜单
<div style="width: 500px;height: 500px;border:solid 1px black;" @contextmenu="rightClick">
    组件方式打开菜单
  </div>
  <vue3-menus v-model:open="isOpen" :event="eventVal" :menus="menuss" hasIcon>
    <template #icon="{ item: { activeIndex } }">{{ activeIndex }}</template>
    <template #label="{ item: { item } }">插槽:{{ item.label }}</template>
  </vue3-menus>

let isOpen = ref(false);
const eventVal:any = ref({});
const rightClicks = (event: any) => {
  isOpen.value = false;
  nextTick(() => {
    eventVal.value = event;
    isOpen.value = true;
  })
  event.preventDefault();
}
const menuss = shallowRef([
  {
    label: "复制",
    tip: '复制改内容',
    click: () => {
      console.log('复制了');
    }
  },
  {
    label: "关闭",
    click: () => {
      console.log('关闭了');
    }
  }
]);
//最后附上最终效果图

 

 
三、整体代码

<template>
  <!-- <div style="width: 500px;height: 500px;border:solid 1px black;" v-menus:right="menus">
    指令方式打开菜单
  </div> -->
  <!-- <div style="width: 500px;height: 500px;border:solid 1px black;" @click.stop @contextmenu="rightClick">
    事件方式打开菜单
  </div> -->
  <div style="width: 500px;height: 500px;border:solid 1px black;" @contextmenu="rightClick">
    组件方式打开菜单
  </div>
  <vue3-menus v-model:open="isOpen" :event="eventVal" :menus="menuss" hasIcon>
    <template #icon="{ item: { activeIndex } }">{{ activeIndex }}</template>
    <template #label="{ item: { item } }">插槽:{{ item.label }}</template>
  </vue3-menus>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted, defineComponent, shallowRef, nextTick } from 'vue'
import { menusEvent, directive, Vue3Menus } from 'vue3-menus';
const vMenus = directive

let isOpen = ref(false);
const eventVal:any = ref({});
const rightClicks = (event: any) => {
  isOpen.value = false;
  nextTick(() => {
    eventVal.value = event;
    isOpen.value = true;
  })
  event.preventDefault();
}
const menuss = shallowRef([
  {
    label: "复制",
    tip: '复制改内容',
    click: () => {
      console.log('复制了');
    }
  },
  {
    label: "关闭",
    click: () => {
      console.log('关闭了');
    }
  }
]);
const menus = shallowRef({
  menus: [
    {
      label: "复制",
      tip: '复制改内容',
      click: () => {
        console.log('复制了');
      }
    },
    {
      label: "关闭",
      click: () => {
        console.log('关闭了');
      }
    }
  ]
});
const rightClick = (event: any) => {
  menusEvent(event, menus.value, 0);
  event.preventDefault();
}
</script>
<style scoped lang="scss"></style>

四、总结
一个简单的demo,使用方法比较简单,自身支持ts,右键后的逻辑和相关业务可以自己去补充调整。希望这个分享能帮助到像我一样的小白,哦对,最后附上官方gitee库地址https://gitee.com/longxinziyan/vue3-menus

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先需要安装一些依赖: ```bash npm install electron electron-context-menu vite vue@next vue-router@next @vue/compiler-sfc typescript tslib --save-dev ``` 然后在 `src` 目录下新建 `main.ts` 和 `renderer.ts` 两个文件。 `main.ts` 的内容如下: ```ts import { app, BrowserWindow, Menu } from 'electron'; import path from 'path'; import contextMenu from 'electron-context-menu'; // 创建窗口 function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { // 允许使用 node.js nodeIntegration: true, // 允许使用 Electron 的 remote 模块 enableRemoteModule: true, // 允许在渲染进程中使用上下文菜单 contextIsolation: false, }, }); // 加载页面 win.loadFile(path.join(__dirname, '../renderer/index.html')); // 打开开发者工具 win.webContents.openDevTools(); // 设置窗口菜单 const template = [ { label: '菜单1', submenu: [ { label: '子菜单1', click: () => { console.log('点击了子菜单1'); }, }, { label: '子菜单2', click: () => { console.log('点击了子菜单2'); }, }, ], }, { label: '菜单2', click: () => { console.log('点击了菜单2'); }, }, ]; const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); } // 当 Electron 初始化完成并准备好创建窗口时调用这个函数 app.whenReady().then(() => { // 注册上下文菜单 contextMenu({ window: BrowserWindow.getFocusedWindow(), showInspectElement: true, }); createWindow(); // 如果所有窗口都关闭了,退出应用 app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); }); // 在 macOS 上,当单击 Dock 图标并且没有其他窗口打开时,重新创建一个窗口 app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); ``` `renderer.ts` 的内容如下: ```ts import { createApp } from 'vue'; import App from './App.vue'; createApp(App).mount('#app'); ``` 接下来在 `src` 目录下新建 `index.html` 文件,并且在里面添加一个 `div` 元素,并且指定它的 `id` 为 `app`。同时在 `body` 元素下添加如下代码: ```html <script src="./renderer.js"></script> ``` 最后在 `package.json` 中添加如下脚本: ```json { "scripts": { "start": "vite", "build": "vite build", "electron": "electron ." } } ``` 现在可以运行 `npm run start` 来启动应用程序,然后运行 `npm run electron` 来启动 Electron 应用程序。现在你应该已经能够看到一个 Electron 窗口,并且它包含一个菜单。如果你右键单击窗口,你应该能够看到一个上下文菜单
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值