Skip to content

说明

LtContextmenu的props参数定义如下:

ts
type MenuProps = {
    menuOptions: MenuGenericOption
    menuStyle?: MenuStyle
    menuTheme?: MenuTheme
    menuSize?: MenuSize
    width?: string | number
    maxWidth?: string | number
    groupClass?: string | ((menuParam?: any) => string)
    groupStyle?: CSSProperties | ((menuParam?: any) => CSSProperties)
    itemClass?: string | ((menuParam?: any, itemOption?: MenuOption) => string)
    itemStyle?: CSSProperties | ((menuParam?: any, itemOption?: MenuOption) => CSSProperties)
    beforeClose?: (close: () => void) => void
}
参数说明必填默认
menuOptions菜单配置,支持菜单项或菜单组项的数组
menuStyle菜单类型,'google' | 'edge''google'
menuTheme菜单主题,'light' | 'dark-element' | 'dark-naive''light'
menuSize菜单尺寸,'small' | 'normal' | 'large''small'
width菜单宽度,如果没有则自适应
maxWidth菜单最大宽度
groupClass菜单组项的class
groupStyle菜单组项的style
itemClass菜单项的class
itemClass菜单项的style
itemClass关闭前的回调,可终止菜单的关闭

基础菜单

右键点击我
vue
<script setup lang="ts">
import { h, ref } from 'vue'
import 'lt-contextmenu/dist/style.css'
import { LtContextmenu, MenuGenericOption } from 'lt-contextmenu'
import IconRefresh from "./icons/IconRefresh.vue"
import IconDetail from "./icons/IconDetail.vue"
import IconAdd from "./icons/IconAdd.vue"
import IconModify from "./icons/IconModify.vue"
import IconDelete from "./icons/IconDelete.vue"

const tlContextmenuRef = ref<InstanceType<typeof LtContextmenu>>()

const menuOptions = ref<MenuGenericOption>([
    {
        group: "group1",
        options: [
            { id: "1", icon: () => h(IconRefresh), label: "刷新页面" },
            { id: "2", icon: () => h(IconDetail), label: "查看详情" }
        ]
    },
    {
        group: "group2",
        options: [
            { id: "3", icon: () => h(IconAdd), label: "新增" },
            { id: "4", icon: () => h(IconModify), label: "修改" },
            { id: "5", icon: () => h(IconDelete), label: "删除" }
        ]
    }
])
</script>

<template>
    <div class="container" @click.right="(event) => tlContextmenuRef?.open(event)">
        右键点击我
        <LtContextmenu ref="tlContextmenuRef" :menu-options="menuOptions" />
    </div>
</template>

<style scoped>
.container {
    width: 200px;
    height: 200px;
    background-color: #00C9A7;
    color: white;
}
</style>

分组菜单

右键点击我
vue
<script setup lang="ts">
import { h, ref } from 'vue'
import 'lt-contextmenu/dist/style.css'
import { LtContextmenu, MenuGenericOption } from 'lt-contextmenu'
import IconRefresh from "./icons/IconRefresh.vue"
import IconDetail from "./icons/IconDetail.vue"
import IconAdd from "./icons/IconAdd.vue"
import IconModify from "./icons/IconModify.vue"
import IconDelete from "./icons/IconDelete.vue"

const tlContextmenuRef = ref<InstanceType<typeof LtContextmenu>>()

const menuOptions = ref<MenuGenericOption>([
    {
        group: "group1",
        options: [
            { id: "1", icon: () => h(IconRefresh), label: "刷新页面" },
            { id: "2", icon: () => h(IconDetail), label: "查看详情" }
        ]
    },
    {
        group: "group2",
        options: [
            { id: "3", icon: () => h(IconAdd), label: "新增" },
            { id: "4", icon: () => h(IconModify), label: "修改" },
            { id: "5", icon: () => h(IconDelete), label: "删除" }
        ]
    }
])
</script>

<template>
    <div class="container" @click.right="(event) => tlContextmenuRef?.open(event)">
        右键点击我
        <LtContextmenu ref="tlContextmenuRef" :menu-options="menuOptions" />
    </div>
</template>

<style scoped>
.container {
    width: 200px;
    height: 200px;
    background-color: #00C9A7;
    color: white;
}
</style>

子菜单

右键点击我
vue
<script setup lang="ts">
import { h, ref } from 'vue'
import 'lt-contextmenu/dist/style.css'
import { LtContextmenu, MenuGenericOption } from 'lt-contextmenu'
import IconRefresh from "./icons/IconRefresh.vue"
import IconDetail from "./icons/IconDetail.vue"
import IconMore from "./icons/IconMore.vue"
import IconAdd from "./icons/IconAdd.vue"
import IconModify from "./icons/IconModify.vue"
import IconDelete from "./icons/IconDelete.vue"

const tlContextmenuRef = ref<InstanceType<typeof LtContextmenu>>()

const menuOptions = ref<MenuGenericOption>([
    {
        group: "group1",
        options: [
            { id: "1", icon: () => h(IconRefresh), label: "刷新页面" },
            { id: "2", icon: () => h(IconDetail), label: "查看详情" }
        ]
    },
    {
        group: "group2",
        options: [
            {
                id: '3',
                icon: () => h(IconMore),
                label: "操作",
                children: [
                    { id: "3-1", icon: () => h(IconAdd), label: "新增" },
                    { id: "3-2", icon: () => h(IconModify), label: "修改" },
                    { id: "3-3", icon: () => h(IconDelete), label: "删除" }
                ]
            }
        ]
    }
])
</script>

<template>
    <div class="container" @click.right="(event) => tlContextmenuRef?.open(event)">
        右键点击我
        <LtContextmenu ref="tlContextmenuRef" :menu-options="menuOptions" />
    </div>
</template>

<style scoped>
.container {
    width: 200px;
    height: 200px;
    background-color: #00C9A7;
    color: white;
}
</style>

Released under the MIT License.