Vuejs在已安装的生命周期钩子中起作用

时间:2018-03-24 21:28:51

标签: vue.js vuetify.js

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

我的代码如下:

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 个答案:

答案 0 :(得分:2)

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

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