Vuetify – 监听 v-menu 激活器?

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

Vuetify - Listen for v-menu activator?

问题

我想在Vuetify的v-menu打开时执行一些操作,我该如何做呢?我可以以某种方式监视激活器吗?

英文:

I would like to do some actions when the v-menu is opened up in Vuetify? How can I do that? Can I watch the activator somehow?

答案1

得分: 0

感谢你,Jacek,

像这样监听 v-menu 的 v-model。

<template>
  <div class="text-center">
    <v-menu offset-y v-model="menu_model">
      <template v-slot:activator="{ on }">
        <v-btn color="primary" dark v-on="on">{{ buttonText }}</v-btn>
      </template>
      <v-list>
        <v-list-item v-for="(item, index) in items" :key="index">
          <v-list-item-title>{{ item.title }}</v-list-item-title>
        </v-list-item>
      </v-list>
    </v-menu>
  </div>
</template>
<script>
export default {
  data: () => ({
    items: [
      { title: "Click Me" },
      { title: "Click Me" },
      { title: "Click Me" },
      { title: "Click Me 2" }
    ],
    menu_model: "",
    buttonText: "Click to open menu"
  }),
  watch: {
    menu_model(menu_open) {
      if (menu_open === true) {
        this.buttonText = "Menu Open";
      } else {
        this.buttonText = "Click to open menu";
      }
    }
  }
};
</script>

CodePen

英文:

Thank you jacek,

Listen to the v-model of the v-menu like this.

&lt;template&gt;
  &lt;div class=&quot;text-center&quot;&gt;
    &lt;v-menu offset-y v-model=&quot;menu_model&quot;&gt;
      &lt;template v-slot:activator=&quot;{ on }&quot;&gt;
        &lt;v-btn color=&quot;primary&quot; dark v-on=&quot;on&quot;&gt;{{ buttonText }}&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;&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;/template&gt;

&lt;script&gt;
export default {
  data: () =&gt; ({
    items: [
      { title: &quot;Click Me&quot; },
      { title: &quot;Click Me&quot; },
      { title: &quot;Click Me&quot; },
      { title: &quot;Click Me 2&quot; }
    ],
    menu_model: &quot;&quot;,
    buttonText: &quot;Click to open menu&quot;
  }),
  watch: {
    menu_model(menu_open) {
      if (menu_open === true) {
        this.buttonText = &quot;Menu Open&quot;;
      } else {
        this.buttonText = &quot;Click to open menu&quot;;
      }
    }
  }
};
&lt;/script&gt;

CodePen

答案2

得分: 0

根据Vuetify 2.6.3,我们可以使用活动器插槽中的'value'。这是链接

这是到Codepen的链接。

<template>
  <div id="app">
    <v-app id="inspire">
      <div class="text-center">
        <v-menu offset-y v-model="menu_model">
          <template v-slot:activator="{ on, value }">
            <v-btn
              color="primary"
              dark
              v-on="on">
              {{ value ? 'Click to open menu' : 'Menu Open' }}
            </v-btn>
          </template>
          <v-list>
            <v-list-item
              v-for="(item, index) in items"
              :key="index">
              <v-list-item-title>{{ item.title }}</v-list-item-title>
            </v-list-item>
          </v-list>
        </v-menu>
      </div>
    </v-app>
  </div>
</template>
<script>
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    items: [
      { title: 'Click Me' },
      { title: 'Click Me' },
      { title: 'Click Me' },
      { title: 'Click Me 2' },
    ],
    menu_model: false,
  }),
})
</script>
英文:

As per Vuetify 2.6.3, we can make use of 'value' from activator slot. Here is the link

Here is the link to Codepen

&lt;template&gt;
&lt;div id=&quot;app&quot;&gt;
&lt;v-app id=&quot;inspire&quot;&gt;
&lt;div class=&quot;text-center&quot;&gt;
  &lt;v-menu offset-y v-model=&quot;menu_model&quot;&gt;
    &lt;template v-slot:activator=&quot;{ on, value }&quot;&gt;
      &lt;v-btn
        color=&quot;primary&quot;
        dark
        v-on=&quot;on&quot;&gt;
        {{ value ? &#39;Click to open menu&#39; : &#39;Menu Open&#39; }}
      &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;&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;
&lt;/template&gt;

&lt;script&gt;
new Vue({
 el: &#39;#app&#39;,
 vuetify: new Vuetify(),
data: () =&gt; ({
 items: [
  { title: &#39;Click Me&#39; },
  { title: &#39;Click Me&#39; },
  { title: &#39;Click Me&#39; },
  { title: &#39;Click Me 2&#39; },
 ],
}),
})

huangapple
  • 本文由 发表于 2020年1月3日 20:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578611.html
匿名

发表评论

匿名网友

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

确定