3

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';
     }
   }
  }
};
ToddT
  • 3,084
  • 4
  • 39
  • 83

1 Answers1

2

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'));
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Wow.. I can't tell what the difference is! Ha.. what's the secret because it works! – ToddT Mar 24 '18 at 21:46
  • 1
    I made a new note in the answer. Another problem. Ah, and the problem with the "hideme" function is that you were calling `this.hideme()` and not `this.hideMe()` (notice the `M`) – acdcjunior Mar 24 '18 at 21:49
  • 1
    Hah, yeah... don't forget to take a break sometimes :D – acdcjunior Mar 24 '18 at 21:53