英文:
Nuxt3/vue3 how to show modal component that is called from header component?
问题
以下是你要翻译的内容:
Header.vue
<template>
<header class="header sticky-top">
<nav class="container-fluid navbar-light bg-light">
<div class="row justify-content-start">
<div class="col text-end p-3">
<button @click="showModal = true" type="button" class="btn">Register</button>
</div>
</div>
<Modal v-show="showModal"/>
</nav>
</header>
</template>
<script lang="ts">
import Modal from '~~/components/Modal.vue';
export default {
components:{
Modal
},
data(){
return{
showModal: false
}
},
}
</script>
DefaultLayout.vue
<template>
<Header />
<main class="p-4 ">
<div class="container-fluid ">
<slot />
</div>
</main>
<Footer />
</template>
<script lang="ts">
import Header from '~~/components/Header.vue';
import Footer from '~~/components/Footer.vue';
export default {
components: {
Footer,
Header
},
}
</script>
index.vue(这是我的主页)
<template>
<default-layout>
<Listings/>
</default-layout>
</template>
<script lang="ts">
import DefaultLayout from '~~/layouts/DefaultLayout.vue';
import Listings from '~~/components/Listings.vue';
export default {
setup() {
},
components: {
DefaultLayout,
Listings
}
}
</script>
Modal.vue
<template>
<Teleport to="body">
<div class="modal show" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</Teleport>
</template>
<script lang="ts">
export default{
props:{
showModal: Boolean
}
}
</script>
英文:
I am using nuxt3/vue3. I am having a difficult time to call the modal based on my header component. I just put the modal inside the header component and use Teleport
function to teleport the modal in the body
based on the showModal
value but it doesnt work. Here is what I tried
Header.vue
<template>
<header class="header sticky-top">
<nav class="container-fluid navbar-light bg-light">
<div class="row justify-content-start">
<div class="col text-end p-3">
<button @click="showModal = true" type="button" class="btn">Register</button>
</div>
</div>
<Modal v-show="showModal"/>
</nav>
</header>
</template>
<script lang="ts">
import Modal from '~~/components/Modal.vue';
export default {
components:{
Modal
},
data(){
return{
showModal: false
}
},
}
</script>
DefaultLayout.vue
<template>
<Header />
<main class="p-4 ">
<div class="container-fluid ">
<slot />
</div>
</main>
<Footer />
</template>
<script lang="ts">
import Header from '~~/components/Header.vue';
import Footer from '~~/components/Footer.vue';
export default {
components: {
Footer,
Header
},
}
</script>
index.vue (which is my homepage)
<template>
<default-layout>
<Listings/>
</default-layout>
</template>
<script lang="ts">
import DefaultLayout from '~~/layouts/DefaultLayout.vue';
import Listings from '~~/components/Listings.vue';
export default {
setup() {
},
components: {
DefaultLayout,
Listings
}
}
</script>
Modal.vue
<template>
<Teleport to="body">
<div class="modal show" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</Teleport>
</template>
<script lang="ts">
export default{
props:{
showModal: Boolean
}
}
</script>
答案1
得分: 2
我的当前处理模态的方法是将模态组件挂载到全局应用程序模板中,并通过Pinia存储变量来控制其可见性:
我使用Composition API,但希望你能明白我的意思。
英文:
My current approach to modals is to mount the modal component into global app template and controll its visibility by a Pinia store variable:
I use Composition API, but hopefully you'll get the idea.
答案2
得分: 0
根据@ellrohir的回应,我已经为Nuxt 3与Pinia和Primevue开发了一个简单的方法。
我的存储文件modalStore.ts:
export const useModalStore = defineStore({
id: 'modal-store',
state: () => {
return {
displayModal: false,
modalTitle: '',
modalText: '',
};
},
actions: {
showModal(title: string, text: string): void {
this.modalTitle = title;
this.modalText = text;
this.displayModal = true;
},
closeModal(): void {
this.displayModal = false;
},
},
});
我的app.vue:
<script setup>
const modalStore = useModalStore();
</script>
<template>
<DialogLogin />
</template>
我的modal DialogLogin:
<script setup>
const modalStore = useModalStore();
</script>
<template>
<Dialog
:header="$t('login')"
v-model:visible="modalStore.displayModal"
:breakpoints="{ '960px': '75vw', '640px': '90vw' }"
:style="{ width: '50vw' }"
:position="position"
:modal="true"
>
<!-- login component form -->
<FormLogin />
<!-- footer template -->
<template #footer>
<Button
:label="$t('cancel')"
icon="pi pi-times"
@click="modalStore.closeModal()"
class="p-button-text"
/>
</template>
</Dialog>
</template>
然后,我使用按钮来激活它:
<Button
label="Login"
icon="pi pi-sign-in"
@click="modalStore.showModal()"
></Button>
英文:
based on the response of @ellrohir, I have developed a simple method for Nuxt 3 with Pinia and Primevue
my store file modalStore.ts
export const useModalStore = defineStore({
id: 'modal-store',
state: () => {
return {
displayModal: false,
modalTitle: '',
modalText: '',
};
},
actions: {
showModal(title: string, text: string): void {
this.modalTitle = title;
this.modalText = text;
this.displayModal = true;
},
closeModal(): void {
this.displayModal = false;
},
},
});
my app.vue
<script setup>
const modalStore = useModalStore();
</script>
<template>
<DialogLogin />
</template>
my modal DialogLogin
<script setup>
const modalStore = useModalStore();
</script>
<template>
<Dialog
:header="$t('login')"
v-model:visible="modalStore.displayModal"
:breakpoints="{ '960px': '75vw', '640px': '90vw' }"
:style="{ width: '50vw' }"
:position="position"
:modal="true"
>
<!-- login component form -->
<FormLogin />
<!-- footer template -->
<template #footer>
<Button
:label="$t('cancel')"
icon="pi pi-times"
@click="modalStore.closeModal()"
class="p-button-text"
/>
</template>
</Dialog>
</template>
then I activate it with a button
<Button
label="Login"
icon="pi pi-sign-in"
@click="modalStore.showModal()"
></Button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论