Answer the question
In order to leave comments, you need to log in
Good afternoon! How to access a specific array of objects and its specific property using Axios and Vue js?
<body>
<div id="app">
<ul>
<div v-for="messages in message">{{ messages.name }}</div>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.11.1/sass.min.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: ''
},
mounted() {
axios
.get('test.json')
.then(response => (this.message = response.data));
}
})
</script>
</body>
есть файл test.json
[
{
"name": "Vasia",
"age" : "33"
},
{
"name": "Ulia",
"age" : "31"
},
{
"name": "Petia",
"age" : "35"
},
{
"name": "Sergey",
"age" : "30"
}
]
Answer the question
In order to leave comments, you need to log in
So you have the same, the entire array is displayed in a loop:
<div v-for="messages in message">{{ messages.name }}</div>
computed: {
thirdName() {
if (this.messages.length >= 3) {
return this.messages[2].name;
}
}
},
and show it when not empty: PS print the whole list:<div v-if="thirdName">Имя: {{ thirdName }}</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question