Vue JS组件 – 当我第二次按下带有@click的按钮时,它不起作用。

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

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 :

&lt;div class=&quot;col-span-12 mb-5&quot; id=&quot;app&quot;&gt;
    &lt;div class=&quot;text-end text-base&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;text-primary hover:text-secondary&quot; @click=&quot;showModal = true&quot;&gt;Verify your email
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;v-modal :open=&quot;showModal&quot;&gt;
        &lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;form_verify_email&quot;&gt;
            @csrf
            &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;{{ auth()-&gt;user()-&gt;email }}&quot;&gt;
            &lt;div class=&quot;&quot;&gt;
                &lt;x-user.form.buttons.primary&gt;
                    {{ __(&#39;Verify Email&#39;) }}
                &lt;/x-user.form.buttons.primary&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/v-modal&gt;
    &lt;x-user.form.inputs name=&quot;email&quot; placeholder=&quot;Email&quot; type=&quot;email&quot; :value=&quot;old(&#39;email&#39;, auth()-&gt;user()-&gt;email)&quot;/&gt;
&lt;/div&gt;

JS :

import { createApp } from &#39;vue&#39;

import modal from &#39;./components/modal.vue&#39;

// Vue app
const app = createApp({
    data() {
        return {
            showModal: false,
        }
    },
})


app.component(&#39;v-modal&#39;, modal)

app.mount(&#39;#app&#39;)

Vue:

&lt;script setup&gt;
import { ref, defineProps,  onMounted } from &#39;vue&#39;
import { Dialog, DialogPanel, DialogTitle, TransitionChild, TransitionRoot } from &#39;@headlessui/vue&#39;

let { open } = defineProps({
    open: {
        type: Boolean,
        required: true,
    },
})
&lt;/script&gt;

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

&lt;Dialog :open=&quot;open&quot; @close=&quot;$emit(&#39;close&#39;)&quot;&gt;

https://headlessui.com/vue/dialog

以及你的父组件:

&lt;div class=&quot;col-span-12 mb-5&quot; id=&quot;app&quot;&gt;
    &lt;div class=&quot;text-end text-base&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;text-primary hover:text-secondary&quot; @click=&quot;showModal = true&quot;&gt;Verify your email
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;v-modal :open=&quot;showModal&quot; @close=&quot;showModal = false&quot;&gt;
        &lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;form_verify_email&quot;&gt;
            @csrf
            &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;{{ auth()-&gt;user()-&gt;email }}&quot;&gt;
            &lt;div class=&quot;&quot;&gt;
                &lt;x-user.form.buttons.primary&gt;
                    {{ __(&#39;Verify Email&#39;) }}
                &lt;/x-user.form.buttons.primary&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/v-modal&gt;
    &lt;x-user.form.inputs name=&quot;email&quot; placeholder=&quot;Email&quot; type=&quot;email&quot; :value=&quot;old(&#39;email&#39;, auth()-&gt;user()-&gt;email)&quot;/&gt;
&lt;/div&gt;

注意:代码部分不要翻译,只返回翻译好的部分。

英文:

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:

&lt;Dialog :open=&quot;open&quot; @close=&quot;$emit(&#39;close&#39;)&quot;&gt;

https://headlessui.com/vue/dialog

And your parent component:

&lt;div class=&quot;col-span-12 mb-5&quot; id=&quot;app&quot;&gt;
    &lt;div class=&quot;text-end text-base&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;text-primary hover:text-secondary&quot; @click=&quot;showModal = true&quot;&gt;Verify your email
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;v-modal :open=&quot;showModal&quot; @close=&quot;showModal = false&quot;&gt;
        &lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;form_verify_email&quot;&gt;
            @csrf
            &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;{{ auth()-&gt;user()-&gt;email }}&quot;&gt;
            &lt;div class=&quot;&quot;&gt;
                &lt;x-user.form.buttons.primary&gt;
                    {{ __(&#39;Verify Email&#39;) }}
                &lt;/x-user.form.buttons.primary&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/v-modal&gt;
    &lt;x-user.form.inputs name=&quot;email&quot; placeholder=&quot;Email&quot; type=&quot;email&quot; :value=&quot;old(&#39;email&#39;, auth()-&gt;user()-&gt;email)&quot;/&gt;
&lt;/div&gt;

答案2

得分: 0

@click=&quot;showModal = !showModal&quot; 可以翻译为:@click="showModal = !showModal"

英文:

Try this:

@click=&quot;showModal = !showModal&quot;

huangapple
  • 本文由 发表于 2023年7月13日 19:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678905.html
匿名

发表评论

匿名网友

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

确定