Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs function in mounted lifecycle hook

I would like my hideMe() function to be called during the mounted lifecycle hook in my Vuejs code. Currently it is not, and I don't understand why.

My code is below:

export default {
  data() {
    return {
      show: "initial"
    }
  }, 
  methods: {
    hideMe() {
     if(this.$vuetify.breakpoint.name == 'xs') {
        console.log("When is this rendered? " + this.$vuetify.breakpoint.name == 'xs');
        this.show = "none";
     }
   }
  },
  mounted() {
    this.hideme();
    console.log("This is a breakpoint name " + this.$vuetify.breakpoint.name);
    console.log(this.show);
  },
  computed: {
    imageHeight () {
     switch (this.$vuetify.breakpoint.name) {
       case 'xs': return '450px';
       case 'sm': return '500px';
       case 'md': return '500px';
       case 'lg': return '540px';
       case 'xl': return '540px';
     }
   }
  }
};
like image 461
ToddT Avatar asked Jun 02 '26 06:06

ToddT


1 Answers

Your logic is correct, try:

  methods: {
    hideMe() {
     if(this.$vuetify.breakpoint.name == 'xs') {
        console.log("When is this rendered? " + this.$vuetify.breakpoint.name == 'xs');
        this.show = "none";
     }
   }
  },
  mounted() {
    this.hideMe();
    console.log("This is a breakpoint name " + this.$vuetify.breakpoint.name);
    console.log(this.show);
  },

Note: The statement:

 console.log("When is this rendered? " + this.$vuetify.breakpoint.name == 'xs');

Will simply print false because it is working like

 console.log( ("When is this rendered? " + this.$vuetify.breakpoint.name) == 'xs');

Fix it adding ()s:

 console.log("When is this rendered? " + (this.$vuetify.breakpoint.name == 'xs'));
like image 182
acdcjunior Avatar answered Jun 04 '26 23:06

acdcjunior