在Vue3和Vuetify3中使用右键单击打开一个下拉菜单。

huangapple go评论131阅读模式
英文:

Open a dropdown with right click in Vue3 and Vuetify3

问题

我正在尝试处理Vue3的上下文菜单。我想通过右键单击下拉按钮来显示v-menu下的项目列表。我正在参考此链接,但当我尝试在我的Vue3文件中做同样的事情时,出现了如下错误-

在Vue3和Vuetify3中使用右键单击打开一个下拉菜单。

有人能建议我哪里出错了吗?以下是代码-

<v-menu offset-y>
  <template v-slot:activator="{ props }">
    <v-btn
      color="primary"
      dark
      v-bind="props"
      @contextmenu.prevent="props.click"
    >
      Dropdown
    </v-btn>
  </template>
  <v-list>
    <v-list-item
      v-for="(item, index) in menuClick"
      :key="index"
      @click=""
    >
      <v-list-item-title>{{ item.title }}</v-list-item-title>
    </v-list-item>
  </v-list>
</v-menu>
英文:

I am trying to work on the context menu for Vue3. I want to show the list of items under the v-menu by right-clicking on a dropdown button. I am working on this link as a reference but when I tried to do the same in my Vue3 file, It gives me an error like this-

在Vue3和Vuetify3中使用右键单击打开一个下拉菜单。

Can anyone suggest what am I doing wrong? Here is the code-

&lt;v-menu offset-y&gt;
  &lt;template v-slot:activator=&quot;{ props }&quot;&gt;
    &lt;v-btn
      color=&quot;primary&quot;
      dark
      v-bind=&quot;props&quot;
      @contextmenu.prevent=&quot;props.click&quot;
      &gt;
      Dropdown
    &lt;/v-btn&gt;
  &lt;/template&gt;
  &lt;v-list&gt;
    &lt;v-list-item
      v-for=&quot;(item, index) in menuClick&quot;
      :key=&quot;index&quot;
      @click=&quot;&quot;
      &gt;
      &lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
    &lt;/v-list-item&gt;
  &lt;/v-list&gt;
&lt;/v-menu&gt;

答案1

得分: 0

无法发现Vue3文档和Vuetify的菜单API中有context menu事件。但是,我心中有一个替代方案可以完成这个任务。你可以使用.right修饰符与v-model一起打开右键单击按钮的菜单。

以下是策略-

  1. 使用v-model来控制v-menu的切换状态。
  2. 在右键单击时,将v-model设置为true,以打开菜单。
  3. 在左键单击(默认)时,将v-model设置为false。(只有在不想在默认点击时打开菜单时才执行此步骤。)

以下是一个运行示例-

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->
const { createApp } = Vue
const { createVuetify } = Vuetify

const vuetify = createVuetify()

const app = createApp({
  data: () => ({
    menu: false,
    items: [
      { title: 'Click Me' },
      { title: 'Click Me' },
      { title: 'Click Me' },
      { title: 'Click Me 2' },
    ],
  }),
}).use(vuetify).mount('#app')

<!-- language: lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.css">
<div id="app">
  <v-app>
    <div class="d-flex justify-space-around mb-5 font-weight-bold">
      I will open only with the right-click.
    </div>
    <div class="d-flex justify-space-around">
      <v-menu v-model="menu">
        <template v-slot:activator="{ props }">
          <v-btn
            color="primary"
            v-bind="props"
            @click.left.prevent="menu = false"
            @click.right.prevent="menu = true"
          >
            Dropdown
          </v-btn>
        </template>
        <v-list>
          <v-list-item
            v-for="(item, index) in items"
            :key="index"
            :value="index"
          >
            <v-list-item-title>{{ item.title }}</v-list-item-title>
          </v-list-item>
        </v-list>
      </v-menu>
    </div>
  </v-app>
</div>

<!-- end snippet -->

希望这能帮助你实现右键单击按钮打开菜单的功能。

英文:

I couldn't discover any context menu event in Vue3's doc and Vuetify's menu API. Although, I have an alternative in my mind that can do this job. You can utilize the .right modifier sync with v-model to open the menu with the right click of a button.

Here is the strategy-

  1. Use v-model to control the v-menu toggling status.
  2. On the right-click, set the v-model to true so the menu will be open.
  3. On the left-click (default) set the v-model to false. (Do this step only if you don't want to open the menu on the default click.)

Here is a functioning demo-

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const { createApp } = Vue
const { createVuetify } = Vuetify

const vuetify = createVuetify()

const app = createApp({
  data: () =&gt; ({
    menu: false,
    items: [
      { title: &#39;Click Me&#39; },
      { title: &#39;Click Me&#39; },
      { title: &#39;Click Me&#39; },
      { title: &#39;Click Me 2&#39; },
    ],
  }),
}).use(vuetify).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.css&quot;&gt;
&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;div class=&quot;d-flex justify-space-around mb-5 font-weight-bold&quot;&gt;
      I will open only with the right-click.
    &lt;/div&gt;
    &lt;div class=&quot;d-flex justify-space-around&quot;&gt;
      &lt;v-menu v-model=&quot;menu&quot;&gt;
        &lt;template v-slot:activator=&quot;{ props }&quot;&gt;
          &lt;v-btn
            color=&quot;primary&quot;
            v-bind=&quot;props&quot;
            @click.left.prevent=&quot;menu = false&quot;
            @click.right.prevent=&quot;menu = true&quot;
            &gt;
            Dropdown
          &lt;/v-btn&gt;
        &lt;/template&gt;
        &lt;v-list&gt;
          &lt;v-list-item
            v-for=&quot;(item, index) in items&quot;
            :key=&quot;index&quot;
            :value=&quot;index&quot;
            &gt;
            &lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
          &lt;/v-list-item&gt;
        &lt;/v-list&gt;
      &lt;/v-menu&gt;
    &lt;/div&gt;
  &lt;/v-app&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 08:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定