Animating dynamic components in Vue.js using transitions

I am working on implementing transitions for dynamic components in vue.js v2.6.11. I have thoroughly reviewed the documentation on Transitioning Between Components, but I am facing difficulties in achieving the desired results.

To troubleshoot, I have created a sample on codesandbox with various trial and error attempts. Below is the essential code snippet that should ideally facilitate the transition.

template

<template>
  <div id="app">
    <button @click="toggle = !toggle">Toggle</button>
    <transition class="component-fade" mode="out-in">
      <component :is="currentComp" val="msg" :key="currentComp" />
    </transition>
    <transition class="component-fade" mode="out-in">
      <div v-if="toggle">test</div>
      <p v-else>Sorry, no items found.</p>
    </transition>
  </div>
</template>

script

<script>
export default {
  name: "App",
  data() {
    return {
      toggle: true
    };
  },
  components: {
    home2: {
      template: `<h1>home2</h1>`,
    },
    "about-us2": {
      template: `<h1>about2</h1>`,
    },
  },
  computed: {
    currentComp() {
      return this.toggle ? "home2" : "about-us2";
    },
  },
};
</script>

Style

<style scoped>
.component-fade-enter-active,
.component-fade-leave-active {
  transition: all 6s ease;
}

.component-fade-enter,
.component-fade-leave-to {
  opacity: 0;
}
</style>

Explanation of code: Upon clicking the toggle button, the components will switch based on the computed value using the :is attribute of <component>. A basic transition effect (opacity change) is applied during this component switch.

Please let me know if there are any aspects in my code that need improvement or modification.

Answer №1

Please provide the correct transition name.

Update class="component-fade"

<transition class="component-fade" mode="out-in">

to

<transition name="component-fade" mode="out-in">

Check out the corrected sandbox here

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

Should storing a Vue.js instance in sessionStorage be considered an appropriate practice?

Looking to set up a global error handler with axios by displaying a prompt, but the prompt component is tied to the Vue instance. In order to achieve this, I have implemented the global error handler like so: mounted() { SessionStorage.vueInstance = ...

Enhancing webpage performance by updating CSS properties dynamically based on mouse movement using JavaScript

My jQuery function is changing the background-position property of three elements when the user moves their mouse, but it's causing some performance problems. It's worth mentioning that the background images of these elements are SVGs. Here&apo ...

What is the best way to include a file attachment using a relative path in Nodemailer?

I am currently utilizing the html-pdf converter plugin to transform an HTML page into a PDF file. After conversion, this plugin automatically saves the PDF to the downloads folder. When I attempt to attach a PDF to a nodemailer email, my code looks someth ...

What is the best way to execute multiple functions concurrently within a loop using NodeJs?

I've been trying to figure out how to run 3 identical functions simultaneously in a loop, allowing one set to finish before moving on to the next set. I believe this involves using a loop and the Promise API. However, my solution has been unsuccessful ...

Can you explain the distinction between these two forms of functional components in ReactJs?

What sets apart these two code usages? In the FirstExample, focus is lost with every input change (It appears that each change triggers a rerender).. The SecondExample maintains focus and functions as intended. example import React, { useState } from &quo ...

gathering information from a group of items and organizing them into a fresh set of items

I have a collection of objects called Lines nestled within the rows array, which is nested within the tabs array. My goal is to extract specific data from the lines array and transfer them into a new array named 'colors'. This is how the initial ...

What could be causing the data to be displaying as undefined when using AJAX

My issue involves making an ajax call and passing an array with data. However, when I attempt to debug the code in the console, it shows that the data is undefined. What could be causing this? ...

Javascript: Harnessing Textbox Arrays for Improved Functionality

Check out the form displayed below. <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="text" name="name[]" id="name"><br> <input type="text" name="name[]" id="name"><br> <input type="fil ...

Adjust the heights of divs dynamically when using slideToggle

Explaining this may be a bit challenging, so please bear with me. The webpage layout includes a column divided into two panels/sections. The aim is to have the secondary panel expand to occupy the remaining space when one of the panels is minimized. When ...

The element was rendered numerous times

I have implemented a conditional rendering logic using a v-if-statement as shown below: <div v-if="resp > 1023"> <PdfViewer /> </div> <div v-else> <PdfViewer /> </div> The issue I am facing is that ...

Testing asynchronously using Jasmine

My Jasmine and RequireJS test setup was going smoothly until I encountered an issue with the context of the functions I was working on. I am conducting Ajax tests, so my first step is to set up a listener for success and then make the service request. Wit ...

Slider for comparing images is currently malfunctioning in Firefox

I recently came across a tutorial on how to create an image comparison slider, and it worked flawlessly on Chrome. However, when I tried to load it on Firefox, the slider didn't respond at all. Below is a snippet of the code: const slider = do ...

Grabbing an element with Cheerio in Node.JS

Struggling to extract the price element from this page: Running the console.log returns nothing, even though I'm certain I'm targeting the correct element. Any advice on how to tackle this issue? var request = require('request'); var ...

The height of the Material UI Paper component is not appropriately matched with the parent component

I am currently working with the Paper component that contains a Card component, and I am trying to make its height fill the entire screen. To simplify the problem, I have provided the following code: import React from "react"; import { makeStyles ...

Automatically activate a button when it comes into view while scrolling

I am currently working in Joomla, which makes it challenging to create a fiddle, but here is the concept: My website displays a mosaic grid of 12 articles upon loading, along with a 'Load More' button. When this button is clicked, an additional ...

Display the respective div when each anchor is clicked

Imagine you have some basic HTML code like this: What I'm aiming to accomplish is opening the corresponding pane for each anchor without needing to check by name. <div class="nav"> <a id="nav1" class="active">Tab 1</a> <a ...

Using Multiline Strings for Arguments

Is there a way to successfully pass multi-line strings containing spaces and tabs as a parameter to an express server? Below is the Express.js code snippet which accepts the parameter: app.get('/:prompt', async (req, res) => { ...

Logging Errors in AngularJS

Currently, I have implemented stacktrace.js to log errors in my Angularjs app like this: app.factory('$exceptionHandler', ['errorLogService',function (errorLogService) { return function (exception, cause) { errorLogService.log( ...

Chrome on IOS: Experiencing screen freezing while scrolling

1) I'm working with Material UI in React JS. I have added a simple scrollable code, but when I reach the bottom of the screen/scroll, it freezes. 2) I also tried it with a simple HTML page and it worked correctly. <div style={{ ...

The slide experiences overflow as it continuously slides up and down

Hey everyone, I've been working on creating a slider effect when a button is clicked. Details about a specific part appear in a designated div upon button click. However, I've encountered a bug where, if I click quickly on different slides, the c ...