javascript - Vue.js consume json -


my problem json. http://dev-rexolution.pantheonsite.io/api/noticias

i need consume vuejs 2 first element of array able display it, working console worked no vuejs.

this console log work: console.log(response.data[0].title[0].value);

<template>   <div class="box box--destacado1">     <div class="media media--rev">       <div class="media-image">           </div>               <div class="media-body">                 <span class="box-info">{{ noticias[0].field_fecha[0].value}}</span>                 <h3 class="box-title">                  <a href="">{{ /*noticias[0].title[0].value */}}</a>                 </h3>                <p class="box-text">{{/*noticias[0].field_resumen[0].value*/}}</p>               </div>             </div> </template>  <script> import axios 'axios';  export default {   data: () => ({     noticias: [],     errors: []   }),    // fetches posts when component created.   created() {     axios.get(`http://dev-rexolution.pantheonsite.io/api/noticias`)     .then(response => {       // json responses automatically parsed.       this.noticias = response.data     })     .catch(e => {       this.errors.push(e)     })   } } </script> 

you're running issue template attempting show data doesn't exist until ajax request has completed.

i set flag indicate when data available , toggle display using v-if. example

template

<div class="media-body" v-if="loaded"> 

script

data () {   loaded: false,   noticias: [],   errors: [] } 

and in created hook

.then(response => {   this.loaded = true   this.noticias = response.data }) 

alternatively, set initial noticias array dummy data

noticias: [{   title: [{ value: null }]   field_fecha: [{ value: null }]   field_resumen: [{ value: null }] }] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -