Stop Full Component Re-rendering with Vue.js

Is there a way to prevent the complete re-rendering of my component, specifically a modal? Currently, when a user logs in, my web application displays a modal with some messages. When the user clicks 'next', the content inside the modal changes. However, the modal's animation pops up again, even though it's the same modal with just different content.

This behavior is typical for Vue, but I'd like to avoid the modal popping up again if only the firstPage property changes. Is there a way to refresh just the content part of the modal, without rendering the entire modal?

<template>
  <div>
    <b-modal v-model="modalShow">
       <p v-if="firstPage"&rt;Hello</p>
       <p v-else>{{content}}</p>
    </b-modal>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        modalShow: false,
      }
    },
    computed() {
      content() {
        return this.$store.state.content
       },
     firstPage() {
        return this.$store.state.firstPage
      }
    }
  }
</script>

Answer №1

It is unlikely that the re-render issue is caused by the children elements. Can you provide more details about the example? In the given code snippet, the content of the children elements changes without reopening the modal for the second time. This behavior seems to be related to an unintended change in the "modalShow" variable. Could you please clarify how and where this data is being manipulated?

window.onload = () => {
  new Vue({
    el: '#app',
    data() {
      return {
        modalShow: false,
        firstPage: true,
      }
    },
    methods: {
        toggle() {
            this.modalShow = !this.modalShow;
            setTimeout(() => this.firstPage = !this.firstPage, 300);
        }
    }
  })
}
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c1ccccd7d0d7d1c2d38ed5d6c6e3918d91928d91">[email protected]</a>/dist/bootstrap-vue.css" rel="stylesheet"/>
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53313c3c27202721322313677d667d60">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ef8fbebcebca0b8a0bfbc">[email protected]</a>/dist/vue.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9c4c4dfd8dfd9cadb86dddeceeb9985999a8599">[email protected]</a>/dist/bootstrap-vue.js"></script>
<script src="https://unpkg.com/babel-polyfill/dist/polyfill.min.js"></script>

<div id="app">
  <div>
    <b-button @click="toggle">Launch demo modal</b-button>

    <b-modal v-model="modalShow">
      <p class="my-4" v-if="firstPage">1st Component</p>
      <p class="my-4" v-else>2nd Component</p>
    </b-modal>
  </div>
</div>

Answer №2

Upon reviewing your inquiry, it seems you are looking to implement a login button. You can achieve this by adding a click event to your button as shown below:

<b-button @click="toggleLogin"> LOGIN </b-button>

In the above code, the boolean value is toggled between true and false each time the button is clicked.

To make this work, you need to define the 'login' property in your data section like so:

data() {
  return {
    login: true,
  }
}

You can then use this property in your template to display different content based on its value:

<p v-if="login == true">Login is true</p>
<p v-else>Login is false</p>

If you prefer, you can also create a method for the click event to handle the logic without directly manipulating the property in the template.

I hope this guidance proves helpful to you! Please feel free to reach out if you have any further questions.

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

Vue fails to properly watch object properties that are set within a for loop

During a for-loop, I am attempting to set an object property and monitor those changes. However, Vue seems to be ignoring some of the changes made to the watched property. To better illustrate my problem, here is a simple test code (https://jsfiddle.net/h ...

Problem: Vue 3 build does not generate service worker

After adding the "@vue/cli-plugin-pwa": "^4.5.12" to my package.json and setting up workbox configuration in my vue.config.js, I encountered an issue. When running npm run build:prd with the script vue-cli-service build --mode prod.exam ...

What is the best way to utilize Gulp to build a Vue component tag within a JavaScript file?

I am trying to use Gulp to build a JavaScript file that is imported into Vue files. The desired code structure is as follows: export const myOrderTable = (h, _this) => { return [ { title: 'orderNo', align: 'center', ...

Can someone help me understand this AngularJS code?

Currently, I'm grappling with a form in Angular that's presenting me with an unusual complication. The form resembles a rating system where users are asked to rate 5 questions on a scale of 1-5. Seems simple enough, right? Add up the scores and ...

Double quotes in JSON are not being escaped properly

When I try to evaluate some json on a screen, I keep encountering errors with the message 'unexpected identifier'... The problematic data that seems to be causing this issue is: "itemDescription":"STANDARD \"B\" RED BOX", To address ...

Understanding how to parse an array of arrays in JavaScript can be a

Looking for a function that can extract a value from an array containing multiple arrays? Simply use getValueFromArray(array, [2, 4]) to get the 4th element of the 2d array within the main array. Check out the code snippet below: function getValueFromArr ...

Sharing edited images from various devices while preserving their original high resolution

My aim is to allow users the ability to upload a profile image and then crop it before uploading to ensure it fits perfectly within the image container. This is a common issue that is encountered frequently. The challenge I am facing arises when a user us ...

Incorporating an AngularJs App into Joomla: A Step-by-

As someone who is currently learning both Angular and Joomla, I am curious about the possibility of integrating an Angular JS Application within Joomla. While Joomla is known for its ease in creating articles and managing content through the admin panel, i ...

Experiencing a surge in requests with LazyLoad enabled?

My website contains numerous images, most of which are displayed within a slider using SlickSlider.js with Lazyload. Although the page load time is 3.87 seconds, there are over 134 requests being made for these images. Upon closer inspection, I noticed th ...

Having trouble with installing create-react-app for my_app using npm because it seems to be stuck on f

I've hit a roadblock while trying to create a react app on my 2011 MacBook Pro. To start, I downloaded the latest version of Node from their official website. Following the instructions from React documentation, I ran the following commands: npm uni ...

Learn how to nest a <div> element within the v-html directive in VueJS

Here is the HTML code that I am currently working with: <div v-html="parse(message.message)"> <i v-if="message.messageTypeId === 2" class="SpecialIcon"></i> </div> I have noticed that the <i v-if="message.messageTypeId === 2 ...

Is there a way to sort search outcomes by a drop-down menu in Next.js?

I am currently working on implementing a filter for my data based on selections made in a drop-down menu. Here's the setup: I have MSSQL data being pulled into NextJS using Prisma (ORM). My goal is to create a dropdown filter that will refine the di ...

What steps should be taken to modify it so that the user is prompted to input the time in minutes

I attempted to modify the user input in minutes by changing "remseconds" to remminutes and adjusting the calculations to "x 60", but unfortunately, nothing happened as expected. Instead of "remseconds," I tried using remminutes and multiplied it by 60, bu ...

Show a condensed version of a lengthy string in a div using React TS

I've been tackling a React component that takes in a lengthy string and a number as props. The goal of the component is to show a truncated version of the string based on the specified number, while also featuring "show more" and "show less" buttons. ...

Unable to retrieve the latest state changes in React

I'm encountering an issue where I am unable to access the updated state in a function. Here's what's happening: There is a getImages() function that gets called either when the component loads for the first time or when the user reaches the ...

Convert an array of objects into JSON format

Presented here is an array containing objects: (the array can contain any number of objects...) [Object, Object] 0: Object 0: "2" 1: "Laza Lazic" id_tabele: "2" naziv: "Laza Lazic" __proto__: Object 1: Object 0: "1" 1: "Pera Per ...

Is it recommended to include the app.js file in the gitignore for a Node.js/Vue.js application?

I'm a beginner with vuejs and I've been facing an issue where it says there's a conflict in the app.js file when I pull. The problem is that the app.js file is quite large, making it difficult to pinpoint the exact cause of the conflict. Wou ...

Prevent screen rotation on any mobile device's browser

Is there a way to lock the screen orientation to landscape for all mobile device browsers when accessing a website? Searching online yields results mainly focused on app development, but I'm interested in achieving this for websites. I've found ...

Vue app with Firebase integration auto-logout on page refresh

I've been working on a vue.js application with Firebase as the backend. I've successfully implemented authentication and a route guard. Despite setting the session persistence to firebase.persistance.LOCAL, I keep getting redirected to the login ...

How to use jQuery to center an overlay on a webpage

Need help with centering a jquery popup on a webpage? I'm struggling with positioning it in the middle of the page, regardless of site adjustments or resolution changes. Currently, I have it set to absolute positioning with CSS, but the issue is that ...