How to suppress “Undefined” errors before fetching data in Vue

别说谁变了你拦得住时间么 提交于 2019-12-02 07:02:35

new Vue({
  el: '#app',
  data: {
    data: {name: 'daniel'},
    data2: {}
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p v-if="data" v-text="data.name"></p>
  <p v-if="data2" v-text="data2.name"></p>
</div>

You need to render it conditionally. Since data from the backend is not accessible from the very beginning, you should add a condition to display element only when the data is ready. See example.

<p>Order's customer name: <span v-if="order" v-text="order.customer.name"></span></p>

I think you can test for order.customer :

{{ order.customer && order.customer.name }}

if order.customer is not defined, the expression evaluation will simply stop at the first term without throwing an error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!