条件错误在vue.js

时间:2018-02-02 06:37:17

标签: javascript vue.js

我正在尝试检查我的变量是否为空。我认为我的代码看起来很好。 但它不起作用..

目前我的vue.js版本为2.5.13

以下是我的代码

<template>
    <div v-if="Object.keys(this.myValues).length === 0">
        is empty
    </div>
    <div v-else>
        good
    </div>
</template>

<script>
export default {
  data: function() {
    return {
      myValues: new Object()
    };
  }
};
</script>

1 个答案:

答案 0 :(得分:1)

这是工作示例data引用当前模板,因此请删除this

&#13;
&#13;
var app = new Vue({
  el: '#el',
  data: function() {
    return {
      myValues: new Object(),
      another: {some:'value'}
    }
  }
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<div id="el">
    <div v-if="Object.keys(myValues).length === 0">
        myValues is empty
    </div>
    <div v-else>
        myValues have some element
    </div>
    
    <div v-if="Object.keys(another).length === 0">
        Another is empty
    </div>
    <div v-else>
        Anoter have some element
    </div>
</div>
&#13;
&#13;
&#13;