Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js Vuejs在数据之前显示评估v-if_Vue.js_Vuejs2 - Fatal编程技术网

Vue.js Vuejs在数据之前显示评估v-if

Vue.js Vuejs在数据之前显示评估v-if,vue.js,vuejs2,Vue.js,Vuejs2,好的,这个问题很模糊,但我有一个代码如下: <template> <div> <p v-if="users" v-for="user in users"> {{ user.name}} </p> <p v-else> No users found</p> </div> </template> <script> export default {

好的,这个问题很模糊,但我有一个代码如下:

<template>
  <div>
      <p v-if="users" v-for="user in users"> {{ user.name}} </p>
      <p v-else> No users found</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        users: null
      }
    },

    created() {
      var that = this 
      axios.get('...').then(response => {
          that.users = response.data
      }).catch(error => { .... })
    }
  }
</script>

{{user.name}

未找到用户

导出默认值{ 数据(){ 返回{ 用户:空 } }, 创建(){ var=this 获取(“…”)。然后(响应=>{ that.users=response.data }).catch(错误=>{….}) } }
因此,actuall脚本没有问题,它加载用户并正确显示。但是,在vuejs加载用户之前,我总是看到没有用户找到
。我不想看到这些消息,除非
users
null
,但vue似乎不会在显示
v-else
之前等待该消息为真


是否有适当的方法来处理此问题,而不是使用用户来执行if/else,请使用加载属性(您可能需要它来向用户显示加载状态):


{{user.name}

未找到用户

导出默认值{ 数据(){ 返回{ 用户:空, 加载:正确 } }, 创建(){ var=this 获取(“…”)。然后(响应=>{ that.users=response.data 这是错误的 }).catch(错误=>{that.loading=false….}) } }
我认为这个代码更好:

<template>
  <div>
      <p v-for="user in users"> {{ user.name}} </p>
      <p v-if="isLoaded && user.length === 0"> No users found</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isLoaded: false,
        users: []
      }
    },

    created() {
      var that = this 
      axios.get('...').then(response => {
          that.users = response.data
          that.isLoaded = true
      }).catch(error => { .... })
    }
  }
</script>

{{user.name}

未找到任何用户

导出默认值{ 数据(){ 返回{ isLoaded:false, 用户:[] } }, 创建(){ var=this 获取(“…”)。然后(响应=>{ that.users=response.data that.isLoaded=true }).catch(错误=>{….}) } }
这似乎是一个丑陋的黑客行为。我曾想过,但这会弄乱我的代码。这不是黑客行为,这是常见的做法,正如我所说,你可能需要它来向用户显示加载消息(你不希望用户在不知道你正在加载数据的情况下等待数据加载),试着将网络设置为减慢3G(在开发工具中)看看你需要等待多长时间如果我可以问,你为什么在axios.then语句中使用这个而不是这个?他也会有同样的问题,因为users.length在加载数据之前是0如果
users
为空,然后,第一个计算表达式,即
user.name
将抛出erro@hidar-不会,因为vue不会呈现此部件您无法像您希望的那样解决此问题:网站加载速度很快-但加载用户是一个异步操作!加载用户可能需要3秒钟-此时没有用户,用户为空。在等待用户列表时,您希望用户在这3秒钟内看到什么?空白页?装载旋转器?
<template>
  <div>
      <p v-for="user in users"> {{ user.name}} </p>
      <p v-if="isLoaded && user.length === 0"> No users found</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isLoaded: false,
        users: []
      }
    },

    created() {
      var that = this 
      axios.get('...').then(response => {
          that.users = response.data
          that.isLoaded = true
      }).catch(error => { .... })
    }
  }
</script>