挂载生命周期钩子中的 Vuejs 函数

Vuejs function in mounted lifecycle hook

我希望在 Vuejs 代码的 mounted 生命周期挂钩期间调用我的 hideMe() 函数。目前不是,我不明白为什么。

我的代码如下:

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

你的逻辑是正确的,试试:

  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);
  },

注: 声明:

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

将简单地打印 false 因为它像

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

修复它添加 ()s:

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