发射事件 Vue.js 3 和 Quasar

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

Emit event vuejs 3 and quasar

问题

<!-- 选项卡 -->
<div v-if="tabs" class="page-toolbar_tabs">
  <q-tabs
    no-caps
    align="left"
    narrow-indicator
    indicator-color="white"
    class="text-white"
    @click="onTabChange"
  >
    <q-route-tab
      v-for="(tab, index) in tabs"
      :key="index"
      :to="tab.path"
      :name="tab.name"
      :label="$t(tab.label)"
      :ripple="false"
      exact
    />
  </q-tabs>
</div>
英文:

How to update this code to start calling the onTabChange method. Emitting the event does not work, the tabs were using the @input method which is currently not triggering anything, it won't display console.log, only when I replace this @input with @click, but then I get pointer event object, not the tab clicked.

<!-- Tabs -->
<div v-if="tabs" class="page-toolbar_tabs">
  <q-tabs
    no-caps
    align="left"
    narrow-indicator
    indicator-color="white"
    class="text-white"
    @input="onTabChange"
  >
    <q-route-tab
      v-for="(tab, index) in tabs"
      :key="index"
      :to="tab.path"
      :name="tab.name"
      :label="$t(tab.label)"
      :ripple="false"
      exact
    />
  </q-tabs>

and in script I have method:

methods: {
    onTabChange (tab) {
      console.log("this is not called")
      this.$emit('onTabChange', tab)
    }
  }

How to make this onTabChange pass the current tab title?

答案1

得分: 1

对于第二种解决方案,您可以在 q-tab 上使用 "@update:model-value" 事件来检测当前选项卡的更改。

英文:

For the second solution, you can use "@update:model-value" event on q-tab for detect current tab changing.

答案2

得分: 0

I have solved this by adding the click event on:

<q-route-tab
  v-for="(tab, index) in tabs"
  :key="index"
  :to="tab.path"
  :name="tab.name"
  :label="$t(tab.label)"
  :ripple="false"
  exact
  @click="onTabClick(tab)"
/>

and then emit the event in a method:

methods: {
    onTabClick (tab) {
      console.log("this is now called", tab.name)
      this.$emit('onTabChange', tab.name)
    }
}
英文:

Ok I have solved this by adding the click event on:

    &lt;q-route-tab
      v-for=&quot;(tab, index) in tabs&quot;
      :key=&quot;index&quot;
      :to=&quot;tab.path&quot;
      :name=&quot;tab.name&quot;
      :label=&quot;$t(tab.label)&quot;
      :ripple=&quot;false&quot;
      exact
      @click=&quot;onTabClick(tab)&quot;
    /&gt;

and then emit the event in a method:

methods: {
    onTabClick (tab) {
      console.log(&quot;this is now called&quot;, tab.name)
      this.$emit(&#39;onTabChange&#39;, tab.name)
    }
  }

huangapple
  • 本文由 发表于 2023年5月21日 16:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298892.html
匿名

发表评论

匿名网友

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

确定