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';
     }
   }
  }
};

1 Answer 1

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'));
Sign up to request clarification or add additional context in comments.

3 Comments

Wow.. I can't tell what the difference is! Ha.. what's the secret because it works!
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)
Hah, yeah... don't forget to take a break sometimes :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.