VUE 3事件/听众无法正确交流

发布于 2025-02-09 18:10:17 字数 3011 浏览 0 评论 0原文

我正在构建VUE应用程序(带有Naive-UI)来记录我的体育投注。我已经创建了一个具有赌注网格的组件,并且我正在尝试创建一种模式,该模态将使我能够创建新的投注。模态将第一次正确打开。但是,当我第二次尝试打开它时,它不会打开。这是因为“ ShowBetModal”仍将“ showbetmodal”设置为true。事件为什么不触发?我已经删除了数据表的内容,以使摘要可管理。

BETS.VUE

<template>
  <div>
    <n-card title="Bets" header id="bets-table-card">
      <template #header-extra>
        <n-icon class="pointer" @click="showBetModal = true">
          <add-circle-outline></add-circle-outline>
        </n-icon>
      </template>
    <bet-modal :showModal="showBetModal"  @closeBetModal="closeBetModal()"/>
  </n-card>
</div>

</template>

<script lang="ts">
import { defineComponent, h, onMounted, ref, reactive } from "vue";
import {
  DataTableColumns,
  NDataTable,
  NSelect,
  PaginationInfo,
  NCard,
  NIcon,
} from "naive-ui";
import { RowData } from "naive-ui/es/data-table/src/interface";
import CurrencyFormatter from "@/components/formatters/CurrencyFormatter.vue";
import PercentageFormatter from "@/components/formatters/PercentageFormatter.vue";
import { useAuth0 } from "@auth0/auth0-vue";
import axios from "axios";
import Bet from "@/models/Bet";
import { AddCircleOutline } from "@vicons/ionicons5";
import BetModal from "@/components/modals/bet-modal.vue";

var data = ref([] as Bet[]);
var loading = ref(false);

export default defineComponent({
name: "Bets",
components: {
NDataTable,
NCard,
AddCircleOutline,
NIcon,
BetModal
},
setup() {
const dataRef = ref([] as Bet[]);
});

const showBetModal = ref(false);
return {
  showBetModal: showBetModal,
  closeBetModal: () => {
    showBetModal.value = false;
  },
    };
  },
});
</script>

和我现在有模态组件,因为我正在传递一个道具来控制模态是打开还是关闭的。我尝试使用它与设置(props,{emit}),与此使用。

best-modal.vue

<template>
   <n-modal :show="show" @close="closeBetModal()">
        <n-card title="Add Bets">
        
            <template #footer>
                <n-button @click="show = false">Cancel</n-button>
                <n-button @click="show = false">Add</n-button>
            </template>
        </n-card>
    </n-modal>
</template>

<script>
import { defineComponent, ref } from "vue";
import { NModal, NButton, NCard } from "naive-ui";
export default defineComponent({
    name: "BetModal",
    components: {
        NModal,
        NButton,
        NCard
    },
    props: {
        showModal: {
            type: Boolean,
            required: true
        },
    },
    emits: ["closeBetModal"],
    setup(props, { emit }) {

        const show = ref(props.showModal);
        const closeBetModal = () => {
            emit("closeBetModal");
        };
        return {
            show: show,
            closeBetModal
        }
    },
    watch: {
        showModal: function(newValue){
           this.show = newValue;
        }
    }

})
</script>

我也看不到事件在vue-devtools中触发。

I'm building a vue application (with naive-ui) to log my sports bets. I've created a component that has a grid of my bets and I'm trying to create a modal that will allow me to create new bets. The modal will open correctly the first time. But when I try open it a second time, it won't open. This is because 'showBetModal' is still set to true in the parent component. Why isn't the event triggering? I've removed the contents of the data-table to keep the snippet manageable.

Bets.vue

<template>
  <div>
    <n-card title="Bets" header id="bets-table-card">
      <template #header-extra>
        <n-icon class="pointer" @click="showBetModal = true">
          <add-circle-outline></add-circle-outline>
        </n-icon>
      </template>
    <bet-modal :showModal="showBetModal"  @closeBetModal="closeBetModal()"/>
  </n-card>
</div>

</template>

<script lang="ts">
import { defineComponent, h, onMounted, ref, reactive } from "vue";
import {
  DataTableColumns,
  NDataTable,
  NSelect,
  PaginationInfo,
  NCard,
  NIcon,
} from "naive-ui";
import { RowData } from "naive-ui/es/data-table/src/interface";
import CurrencyFormatter from "@/components/formatters/CurrencyFormatter.vue";
import PercentageFormatter from "@/components/formatters/PercentageFormatter.vue";
import { useAuth0 } from "@auth0/auth0-vue";
import axios from "axios";
import Bet from "@/models/Bet";
import { AddCircleOutline } from "@vicons/ionicons5";
import BetModal from "@/components/modals/bet-modal.vue";

var data = ref([] as Bet[]);
var loading = ref(false);

export default defineComponent({
name: "Bets",
components: {
NDataTable,
NCard,
AddCircleOutline,
NIcon,
BetModal
},
setup() {
const dataRef = ref([] as Bet[]);
});

const showBetModal = ref(false);
return {
  showBetModal: showBetModal,
  closeBetModal: () => {
    showBetModal.value = false;
  },
    };
  },
});
</script>

And I have the modal component right now that I'm passing a prop to control whether the modal is open or closed. I've tried using it with setup(props, {emit}), inline with this.$emit, and in a function with this.$emit.

bet-modal.vue

<template>
   <n-modal :show="show" @close="closeBetModal()">
        <n-card title="Add Bets">
        
            <template #footer>
                <n-button @click="show = false">Cancel</n-button>
                <n-button @click="show = false">Add</n-button>
            </template>
        </n-card>
    </n-modal>
</template>

<script>
import { defineComponent, ref } from "vue";
import { NModal, NButton, NCard } from "naive-ui";
export default defineComponent({
    name: "BetModal",
    components: {
        NModal,
        NButton,
        NCard
    },
    props: {
        showModal: {
            type: Boolean,
            required: true
        },
    },
    emits: ["closeBetModal"],
    setup(props, { emit }) {

        const show = ref(props.showModal);
        const closeBetModal = () => {
            emit("closeBetModal");
        };
        return {
            show: show,
            closeBetModal
        }
    },
    watch: {
        showModal: function(newValue){
           this.show = newValue;
        }
    }

})
</script>

I can't see the event triggering in the vue-devtools either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文