How can a modal component be displayed in Nuxt3/Vue3 when it is triggered from the header component?

Currently, I am facing a challenge in calling the modal based on my header component while using nuxt3/vue3. I attempted to place the modal inside the header component and utilize the Teleport function to teleport the modal into the body based on the value of showModal, but unfortunately it is not working as expected. Below is an outline of what I have 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 (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>

Answer №2

After receiving feedback from @ellrohir, I have devised a straightforward approach for integrating Nuxt 3 with Pinia and Primevue

This is the code snippet from 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;
        },
    },
});

Here's how it fits into my app.vue

<script setup>
    const modalStore = useModalStore();
</script>
<template>
    <DialogLogin />
</template>

And here's the structure of my modal DialogLogin component

<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>

To activate it, simply use a button like this

<Button
    label="Login"
    icon="pi pi-sign-in"
    @click="modalStore.showModal()"
></Button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vow failing to function as intended

I have encountered an issue with performing a nested query in MySql, saving the result to a variable, and sending it over http. The problem is that the program always executes console.log("test 2:"+rpsData); before the query finishes. Despite attempting ...

Event that occurs when modifying a user's Firebase Authentication details

Monitoring User Actions with Firebase Authentication Within my application built using Angular, Node.js, and Firebase, I am seeking a method to track user events such as additions, modifications, and deletions. Is there a mechanism to recognize when a us ...

What is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...

Guide to Dynamically Including an Element in an Array using Typescript

Encountering a type error within the <RenderFormFields formFields={formFieldsData} /> component:- Types of property 'type' are not compatible. Type 'string' cannot be assigned to type '"select"'.ts(2322) Rende ...

Having trouble accessing data from getters in the router with Vuex and Quasar

I am trying to extract the date from getters in my router. Within my store, I have 2 modules: export default function (/* { ssrContext } */) { const Store = new Vuex.Store({ modules: { customers, auth }, namespaced: true, strict: process.env.DE ...

Tips for identifying and handling a 400 bad request error in an HTTP response within an Angular 2 application

I attempted to handle the error 400 bad request in this manner: catch((error: any) => { if (error.status === 500) { return Observable.throw(new Error(error.status)); } else if (error.status === 400) { console.log( 'err ...

XMLHttpRequest Failing to Retrieve Data - Error Code 202

Currently, I am immersed in a project that involves using a webservice (specifically, VB.Net and Javascript). While debugging the code, I encountered an issue with the XmlHttpRequest.status returning as 202. Upon comparison with my previous webservice proj ...

What is the best way to eliminate YouTube branding from a video embedded in a website?

I am currently utilizing an <iframe width="550" height="314" src="https://www.youtube.com/embed/vidid?modestbranding=1&amp;rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe> This setup removes the "YouTube" logo from the ...

Dynamic content for tooltips in Angular

Currently, I am facing a challenge where I need to dynamically populate content in a tooltip by executing a function with a parameter. This parameter holds the crucial information required to update the tooltip content. The complexity arises from the fact ...

Encountering issues after updating Laravel Mix to the newest version

I recently upgraded my app to Laravel 9 using Laravel Shift. Everything was going smoothly until the upgrade bumped up the laravel-mix version to 6.x. This caused some compilation issues which I managed to fix. However, now when I try to run the frontend, ...

Ways to show chosen items based on specific criteria in element-ui table

Can anyone help me with a problem I'm having? I need to display certain selections based on conditions. For example, I want to hide the selection option for the name 'Jeff'. Check out my code here I've tried using 'v-if', b ...

Implementing ES6 Angular directives with two-way isolated binding

I'm really struggling to understand how isolating scopes function in my code. Interestingly, everything seems to work fine when I remove the scope part of the directive. Can someone please shed some light on what I might be overlooking? export func ...

I'll show you how to implement custom fonts in TailwindCSS and NuxtJS!

Currently, I am in the process of developing a website using NuxtJS along with Tailwind CSS for styling. To incorporate my desired fonts, I have utilized the @nuxtjs/tailwindcss module. Despite applying the correct font-family through CSS, it seems that m ...

error: vue-simplemde npm package does not include markdown-editor

Since upgrading vue-simplemde from npm version 0.5.2 to 2.0.1, I am unable to locate vue-simplemde/src/markdown-editor in my node_modules directory. How can I go about replacing it? ...

React Foundation accordion not functioning properly

Utilizing Foundation accordion within my React component has presented a challenge. The code functions properly when rendering static HTML, but when attempting to render it dynamically through a loop, the accordions lose their clickability. React code fo ...

JavaScript: an array of objects contained within another array of objects

Here is an example dataset for reference: var info = [ {row: 0, col: 0, value: [{x: 1, y: 19}, {x: 2, y: 20}]}, {row: 0, col: 1, value: [{x: 1, y: 24}, {x: 2, y: 27}]}, {row: 1, col: 1, value: [{x: 1, y: 31}, {x: 2, y: 26}]}, {row: 1, col ...

The code is functioning properly, however it is returning the following error: Anticipated either

Can you explain why this code is resulting in an unused expression error? <input style={{margin:'25px 50px 0',textAlign:'center'}} type='text' placeholder='add ToDo' onKeyPress={e =>{(e.key ==='En ...

The onEnter and onExit functions within a React Native Component using react-native-router-flux

Within my app's router definitions, I have successfully utilized the onEnter/onExit method at the root level: <Scene key="arena" hideNavBar={true} onEnter={() => console.log("Entered")} component={ArenaPage} /> Is there a way to achieve th ...

Verify the presence of blank space with javaScript

I currently have a text box on my website. <input type="text" name="FirstName" value="Mickey"> My goal is to prevent the user from entering empty spaces, tabs, or new lines in the text box. However, I want to allow spaces between characters. Does a ...

Exploring Data in Angular 2: Examining Individual Records

I am currently learning Angular and facing some challenges in structuring my questions regarding what I want to achieve, but here is my query. Within a component, I am retrieving a single user record from a service. My goal is to display this user's ...