1

My (vuejs) homepage contains the following:

template: <h3>{{ strings.home_headline }}</h3>

scripts:

data() {
    return {
      strings: []
    };
},
methods: {
    fetchData: function(){
      var self = this;

      // get translatable strings
      axios
        .get(MY_API_URL, {
          headers: { "X-Locale": self.locale }
        })
        .then(function(data) {
          self.strings = data.data;

          // This console works and outputs the right content; 
          console.log("strings: ", self.strings);
          // outputs the text that should come in the H3 
          console.log(strings.home_headline)
        })
        .catch(function(error) {
          console.log("Error", error);
        });
    }
},
created() {
    this.fetchData();
}

the console.log works but the template h3 is empty, UNTIL the first pixels scrolled, then everything is filled in with the text. what am i doing wrong here?

4
  • What is returned from your request? (ie: what your console.log logs) Commented Jul 8, 2020 at 11:36
  • an object with key-value pairs @Finrod Commented Jul 8, 2020 at 11:38
  • I'm not sure it will be enough, but try to define strings as an object, not an array: strings: {} Commented Jul 8, 2020 at 11:41
  • tried that, no change. but agree it makes sense. Commented Jul 8, 2020 at 11:45

2 Answers 2

4

Switching your member from array to object

return {
  // you need the fields here as well - an "empty" prepared object
  strings: { 
    home_headline: "", // more fields go here
  }  
};

and changing the lifecylcle-hook you are using should fix your problem:

async mounted() {
    await this.fetchData();
}

var app = new Vue({
  el: '#app',
  data() {    
    return {
      strings: { home_headline: ""}
    };
  },
  methods: {
    fetchData: function() {
      self = this;
      //will lead to error
      axios
        .get("error", {
          headers: {
            "X-Locale": self.locale
          }
        })
        .then(function(data) {
          self.strings.home_headline = "TATü"; 
        })
        .catch(function(error) {
          self.strings.home_headline = "TATö"; 
        });
    }
  },
  async mounted() {
    await this.fetchData();
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <h3>{{ strings.home_headline }}</h3>
</div>

For vue to enable the change-update you need to specify the fields on the object already.

Regarding the lifecycle hooks: in

created, templates and Virtual DOM have not yet been mounted or rendered. (https://www.digitalocean.com/community/tutorials/vuejs-component-lifecycle#creation-(initialization))

Sign up to request clarification or add additional context in comments.

1 Comment

YES! had a similar problem recently and was explained this one before! many thanks for the explanation, everything now works as expected.
1

can you do this v-if="loading" in your <template>?

<template>
  <div v-if="loading">
    {{ strings }}
  </div>
<template>

your script

data() {
  return {
    loading: false,
    strings: [],
  };
},
created() {
  this.fetchData();
},
methods: {
  fetchData() {
    const self = this;
    self.loading = true;

    // get translatable strings
    axios
      .get(MY_API_URL, {
        headers: { 'X-Locale': self.locale },
      })
      .then((data) => {
        self.strings = data.data;
        self.loading = true;
      })
      .catch((error) => {
        console.log('Error', error);
      });
  },
},

2 Comments

oh! this works, it this normal? i feel like i'm doing something in the code and shouldn't be doing this loading workaround. but again, it works
@bboy It's normal, the view just rendered with v-if . Don't forget to upvote and accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.