【问题标题】:Vuejs function in mounted lifecycle hook已挂载的生命周期挂钩中的 Vuejs 函数
【发布时间】:2018-03-24 21:28:50
【问题描述】:

我希望在我的 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';
     }
   }
  }
};

【问题讨论】:

    标签: vue.js vuetify.js


    【解决方案1】:

    你的逻辑是对的,试试:

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

    【讨论】:

    • 哇.. 我不知道有什么区别!哈.. 有什么秘诀,因为它有效!
    • 我在答案中做了一个新的注释。另一个问题。啊,“hideme”功能的问题是你调用的是this.hideme()而不是this.hideMe()(注意M
    • 哈,是的……有时别忘了休息一下:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2017-01-18
    • 1970-01-01
    • 2021-09-19
    • 2022-01-24
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多