You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt2+Vuetify环境下vue-fortune-wheel外部按钮触发旋转失效求助

解决Nuxt.js 2+Vuetify中vue-fortune-wheel外部按钮触发旋转及条件验证问题

问题分析

你的代码核心问题是错误地通过onImageRotateStart回调函数触发旋转——这个方法是用来响应组件内部rotateStart事件的,并非主动触发旋转的API。同时也未正确实现自定义旋转条件的判断逻辑。

修复方案

1. 给转盘组件添加ref引用

要主动触发旋转,需要通过组件的ref调用内部暴露的startRotate方法,先给<FortuneWheel>加上ref属性:

<FortuneWheel
  ref="fortuneWheel"
  style="width: 500px; max-width: 100%"
  type="image"
  :useWeight="true"
  :verify="verifyRotate" <!-- 替换原布尔值为验证函数 -->
  :prizeId="prizeId"
  :angleBase="-30"
  :duration="6000"
  :timingFun="`cubic-bezier(0,0,.58,1)`"
  :prizes="prizesImage"
  @rotateEnd="onRotateEnd"
>

2. 实现自定义旋转验证逻辑

把原来的布尔值canvasVerify替换成验证函数,用来判断是否允许旋转:

data() {
  return {
    prizeId: 0,
    // ...保留其他原有数据
  }
},
methods: {
  // 自定义验证函数,返回true则允许旋转,false则阻止
  verifyRotate() {
    // 这里替换成你的实际条件,比如检查用户权限、剩余次数等
    const isAllowed = this.prizeId !== 0;
    if (!isAllowed) {
      alert('请先选择一个奖项!');
    }
    return isAllowed;
  },
  // ...保留其他原有方法
}

3. 修改外部按钮的触发方法

更新spinWheel方法,通过ref调用转盘组件的startRotate方法触发旋转:

spinWheel() {
  // 调用组件内部方法触发旋转,会自动执行verify验证
  this.$refs.fortuneWheel.startRotate();
},

4. 清理无用代码

删掉组件外定义的冗余onImageRotateStart函数,组件内的同名方法也可以移除(组件内部会自动处理旋转启动逻辑)。

修改后的完整代码

<template>
  <v-row>
    <v-col cols="12">
      <v-btn @click="spinWheel">Spin Wheel</v-btn>
      <FortuneWheel
        ref="fortuneWheel"
        style="width: 500px; max-width: 100%"
        type="image"
        :useWeight="true"
        :verify="verifyRotate"
        :prizeId="prizeId"
        :angleBase="-30"
        :duration="6000"
        :timingFun="`cubic-bezier(0,0,.58,1)`"
        :prizes="prizesImage"
        @rotateEnd="onRotateEnd"
      >
        <template #wheel>
          <img
            src="/wheel.png"
            style="width: 100%; transform: rotateZ(-90deg)"
          />
        </template>
        <template #button>
          <img src="/button.png" style="width: 180px" />
        </template>
      </FortuneWheel>
      <div class="d-flex rounded-circle" style="width: 500px">
        <v-btn
          class="btn"
          v-for="(item, idx) in prizesCanvas"
          :key="idx"
          :style="{ background: item.bgColor }"
          @click="onChangePrize(item.id)"
        >
          {{ item.name }}
        </v-btn>
      </div>
      <div class="wheel-result">
        current 100%
        <span
          class="px-10"
          :style="{ background: prizeRes.bgColor, border: '1px solid white' }"
          >{{ prizeRes.name }}</span
        >
      </div>
    </v-col>
  </v-row>
</template>

<script>
import FortuneWheel from "vue-fortune-wheel";
import "vue-fortune-wheel/lib/vue-fortune-wheel.css";

export default {
  components: {
    FortuneWheel,
  },
  data() {
    return {
      prizeId: 0,
      canvasOptions: {
        borderWidth: 6,
        borderColor: "#584b43",
      },
      prizesCanvas: [
        {
          id: 1,
          name: "Green",
          value: "Green's value",
          bgColor: "#4CAF50",
          color: "#ffffff",
        },
        {
          id: 2,
          name: "Red",
          value: "Red's value",
          bgColor: "#dd3832",
          color: "#ffffff",
        },
      ],
      prizesImage: [
        {
          id: 1,
          value: "green's value",
          weight: 1,
        },
        {
          id: 2,
          value: "Red's value",
          weight: 0,
        },
      ],
    };
  },
  computed: {
    prizeRes() {
      const result =
        this.prizesCanvas.find((item) => item.id === this.prizeId) ||
        this.prizesCanvas[0];
      return result;
    },
  },
  methods: {
    spinWheel() {
      this.$refs.fortuneWheel.startRotate();
    },
    verifyRotate() {
      // 自定义条件示例:必须选择奖项才能旋转
      const canSpin = this.prizeId !== 0;
      if (!canSpin) {
        alert("请先选择奖项再旋转!");
      }
      return canSpin;
    },
    onChangePrize(prizeId) {
      this.prizeId = prizeId;
    },
    onRotateEnd(prize) {
      alert(prize.value);
    },
  },
};
</script>

关键点说明

  • 通过ref获取组件实例后调用startRotate(),是vue-fortune-wheel@1版本中主动触发旋转的标准方式。
  • verify属性支持传入函数,通过返回布尔值灵活控制旋转权限,完美适配自定义条件判断需求。
  • 移除冗余的事件回调调用,简化代码逻辑的同时避免冲突。

内容的提问来源于stack exchange,提问作者Seng kue vang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:42:33