英文:
Vue JS Components - It doesn't work when I press the button with @click for the second time
问题
I am using Vue JS to create modal in Laravel
when i press the button with @click the first time it works but the next time i get an error.
Laravel Blade :
<div class="col-span-12 mb-5" id="app">
<div class="text-end text-base">
<button type="button" class="text-primary hover:text-secondary" @click="showModal = true">Verify your email
</button>
</div>
<v-modal :open="showModal">
<form action="" method="post" id="form_verify_email">
@csrf
<input type="hidden" name="email" value="{{ auth()->user()->email }}">
<div class="">
<x-user.form.buttons.primary>
{{ __('Verify Email') }}
</x-user.form.buttons.primary>
</div>
</form>
</v-modal>
<x-user.form.inputs name="email" placeholder="Email" type="email" :value="old('email', auth()->user()->email)"/>
</div>
JS :
import { createApp } from 'vue'
import modal from './components/modal.vue'
// Vue app
const app = createApp({
data() {
return {
showModal: false,
}
},
})
app.component('v-modal', modal)
app.mount('#app')
Vue:
<script setup>
import { ref, defineProps, onMounted } from 'vue'
import { Dialog, DialogPanel, DialogTitle, TransitionChild, TransitionRoot } from '@headlessui/vue'
let { open } = defineProps({
open: {
type: Boolean,
required: true,
},
})
</script>
the template I use in tailwindcss : code first
英文:
I am using Vue JS to create modal in Laravel
when i press the button with @click the first time it works but the next time i get an error.
Laravel Blade :
<div class="col-span-12 mb-5" id="app">
<div class="text-end text-base">
<button type="button" class="text-primary hover:text-secondary" @click="showModal = true">Verify your email
</button>
</div>
<v-modal :open="showModal">
<form action="" method="post" id="form_verify_email">
@csrf
<input type="hidden" name="email" value="{{ auth()->user()->email }}">
<div class="">
<x-user.form.buttons.primary>
{{ __('Verify Email') }}
</x-user.form.buttons.primary>
</div>
</form>
</v-modal>
<x-user.form.inputs name="email" placeholder="Email" type="email" :value="old('email', auth()->user()->email)"/>
</div>
JS :
import { createApp } from 'vue'
import modal from './components/modal.vue'
// Vue app
const app = createApp({
data() {
return {
showModal: false,
}
},
})
app.component('v-modal', modal)
app.mount('#app')
Vue:
<script setup>
import { ref, defineProps, onMounted } from 'vue'
import { Dialog, DialogPanel, DialogTitle, TransitionChild, TransitionRoot } from '@headlessui/vue'
let { open } = defineProps({
open: {
type: Boolean,
required: true,
},
})
</script>
the template I use in tailwindcss : code first
can you see where i am going wrong ?
答案1
得分: 1
当你关闭对话框时,showModal
保持为 true
,所以点击不会改变它,第二次什么都不会发生。你应该在关闭 v-modal
事件上将它设置为 false
:
你的 v-modal
:
<Dialog :open="open" @close="$emit('close')">
https://headlessui.com/vue/dialog
以及你的父组件:
<div class="col-span-12 mb-5" id="app">
<div class="text-end text-base">
<button type="button" class="text-primary hover:text-secondary" @click="showModal = true">Verify your email
</button>
</div>
<v-modal :open="showModal" @close="showModal = false">
<form action="" method="post" id="form_verify_email">
@csrf
<input type="hidden" name="email" value="{{ auth()->user()->email }}">
<div class="">
<x-user.form.buttons.primary>
{{ __('Verify Email') }}
</x-user.form.buttons.primary>
</div>
</form>
</v-modal>
<x-user.form.inputs name="email" placeholder="Email" type="email" :value="old('email', auth()->user()->email)"/>
</div>
注意:代码部分不要翻译,只返回翻译好的部分。
英文:
When you close the dialog, showModal
stays true
, so the clicking doesn't change it and nothing happens the second time. You should set it to false
on the close v-modal
event:
Your v-modal
:
<Dialog :open="open" @close="$emit('close')">
https://headlessui.com/vue/dialog
And your parent component:
<div class="col-span-12 mb-5" id="app">
<div class="text-end text-base">
<button type="button" class="text-primary hover:text-secondary" @click="showModal = true">Verify your email
</button>
</div>
<v-modal :open="showModal" @close="showModal = false">
<form action="" method="post" id="form_verify_email">
@csrf
<input type="hidden" name="email" value="{{ auth()->user()->email }}">
<div class="">
<x-user.form.buttons.primary>
{{ __('Verify Email') }}
</x-user.form.buttons.primary>
</div>
</form>
</v-modal>
<x-user.form.inputs name="email" placeholder="Email" type="email" :value="old('email', auth()->user()->email)"/>
</div>
答案2
得分: 0
@click="showModal = !showModal"
可以翻译为:@click="showModal = !showModal"
。
英文:
Try this:
@click="showModal = !showModal"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论