4

Warning: I'm extremely new to Vue.

I'm using it to build a small form widget that can be embedded on various sites for my clients. Each of my clients can customize the form, so I'm using the vue-form-generator package to load a custom schema from my API and then generate the form.

I've gotten it to load data, but I'm not sure how to defer loading of that component until the data has finished loading. I attempted to use v-if, but the property seems to be bound to data, which is where I was doing my data loading.

<template>
  <div id="app">
    <vue-form-generator  v-if="loaded" :schema="schema" :model="model" :options="formOptions"></vue-form-generator>
  </div>
</template>

<script>
  import Vue from 'vue'
  import VueFormGenerator from 'vue-form-generator/dist/vfg-core.js'
  import 'vue-form-generator/dist/vfg-core.css'

  Vue.use(VueFormGenerator)

  export default {
    name: 'app',

    data: function () {
      return fetch('http://localhost:3000/forms')
        .then(function (response) {
          loaded = true;
          return response.json()
        }).then(function (json) {
          return json
        }).catch(function (ex) {
          console.log('parsing failed', ex)
        })
    }
  }
</script>

Is there a better approach here?

Thanks!

1 Answer 1

4

You're returning a promise as your data when the data method needs to return a plain javascript object.

Instead you might do something like this.

export default {
    name: "app",
    data(){
        return {
            loaded: false, 
        }
    },
    created(){
        fetch('http://localhost:3000/forms')
          .then(function (response) {
             return response.json()
           }).then(function (json) {
             this.loaded = true;
             // set other values from json
           }.bind(this)).catch(function (ex) {
             console.log('parsing failed', ex)
           })
    }

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

1 Comment

Thanks, Bert! Much appreciated

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.