0

Such a problem: I created a login template with a login / password form, a function for authorization and a js function in the mounted section, which depicts a beautiful animation. Logically, at the moment of successful authorization, the entire login form, along with the animation, should disappear.

In the app.vue file the html looks like this

<div v-if="isCookies != '' "> 
     <login> </login>
</div>

The problem is that at the moment the isCookies variable is filled, the login form disappears, but the code from its mounted section is still executed and, not finding its canvas in the layout, throws errors into the console. When the page is reloaded, the login form is no longer displayed. cookies are set and the variable is filled, and errors are still pouring in, as if the template was not connected, but the mounted block is still taken from it and processed.

app.vue

<template>

  <div v-if="cookie==''">
     <login @login="login"></login>
  </div>

  <div class="main_window" v-else>
     Some text
  </div>
</template>

<style>
</style>

<script>
  import login from "@/views/login";
export default {
    data() {
       return { cookie:''} 
       },
    async mounted() {
       this.cookie=document.cookie;    
       },
    methods:{
       },
}
 </script>

login.vue

<template>
  <div class="login-panel">
    <input type="login" :placeholder="palceholders.login" @inputEvent="takeInputedLogin">

    </input>

    <br>

    <input type="password" :placeholder="palceholders.pass" @inputEvent="takeInputedPass">

    </input>

    <br>

    <button v-if="authData.login.length>6 && authData.pass.length>5" @click="tryToEnter" >
      телепортация
    </button>
  </div>
  <canvas id="particle-canvas"></canvas>
</template>

<script>
export default {
  name: "login",
  components: {

  },
  async mounted(){
    
   console.log("This code will work even when other part of template will not used. Even if the cookies are not empty on page load.")

  },
  data(){
    return{
      palceholders:{"login":"Name","pass":"Password"},
      authData:{"login":"","pass":""},
    }
  },
  methods:{
    takeInputedLogin(valued){
      this.authData.login=valued;
    },

    takeInputedPass(value){
      this.authData.pass=value;
    },

    tryToEnter(){
      this.$emit("login",this.authData);
    },

  }

}




</script>

<style scoped>

</style>
5
  • Please, post code instead of describing it. See stackoverflow.com/help/mcve Commented Mar 19, 2023 at 19:09
  • Sorry, I wrote the post from a smartphone and forgot that stackoverflow does not process blocks with code correctly - it just hides them if you do it from a smartphone. Commented Mar 19, 2023 at 20:52
  • I see. Why is mounted async? Some code is likely missing. Currently it looks like you're proceeding with the execution of asynchronous routine without aborting it when it's necessary Commented Mar 19, 2023 at 21:07
  • I expected that the code from the template would not be executed at all if this template should not be rendered by v-if. The mount works asynchronously so as not to delay the loading of the DOM. Tried to remove asynchrony and nothing changed. Commented Mar 19, 2023 at 21:59
  • It really won't be executed. Whatever the problem is, it's not shown in the question. Again, the question needs Minimal, Reproducible Example. Commented Mar 20, 2023 at 2:28

1 Answer 1

0

The monted is still running after the login form is removed from DOM. Try to add a boolean variable like this:

export default {
  data() {
    return {
      isLoggedIn: false,
      isAnimated: false,
  
    }
  },
  mounted() {
    if (this.isAnimated) {
      // animation code here
    }
  },
  methods: {
    login() {
      // authorization logic here
      this.isLoggedIn = true;
      this.isAnimated = true;

it's not the correctly but i believe it's works, if not working send your code here

Not the answer you're looking for? Browse other questions tagged or ask your own question.