search

Home  >  Q&A  >  body text

javascript - How to solve the problem that there is a key in the data in the v-for loop in vue but no error is reported

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>

</head>
<body>

<p id="box">
    <ul>
        <li v-for="item in arr">
            <p>{{item.name}}</p>
            <p>{{item.age.age2}}</p>
        </li>
    </ul>
</p>

</body>
</html>
<script type="text/javascript">

var vm = new Vue({
    el:'#box',
    data:{
        arr:[{name:'张三',age:{age2:45},},
            {name:'王五'}]
    }
})

</script>
If part of the data provided by the background does not have the age attribute, an error will be reported through the v-for loop in the document

某草草某草草2796 days ago742

reply all(2)I'll reply

  • 三叔

    三叔2017-06-12 09:31:14

    If it is displayed, it is fine if it is not displayed.

    <li v-for="item in arr">
        <p>{{item.name}}</p>
        <p>{{item.age?item.age.age2:''}}</p>
    </li>

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-12 09:31:14

    There may not be age2
    {{item.age && item.age.age2 ? item.age.age2 :''}}

    reply
    0
  • Cancelreply