1

I have a template:

var playersList = Vue.extend({
    props: ['players'],
    template: '#players-template'
});

Vue.component('players', playersList);

new Vue({
    el: 'body',
    methods: {
        someMethod: function() {
            //JSON data from request comes here
            //Want to render template of players component with that data
        }
    }
});

I'm new with Vue.js and don't know how could I make it possible. How can I render the template with data from AJAX request? Someone posted a solution with v-view but documentation for it is gone at official website.

2 Answers 2

1

You have to specify the data in your Vue instance where the response is going to be stored

var playersList = Vue.extend({
    template: '#players-template',
    props: ['players']
    }
});

Vue.component('players', playersList);

new Vue({
    el: 'body',
    data: {
      players: ''
    },
    methods: {
        someMethod: function() {
            //JSON data from request comes here
            //Want to render template of players component with that data
            this.$set('players', data);
        }
    }
});

in your html
<body>
  <ul>
    <li v-for="player in players">
      <players :players="players"></players>
    </li>
  </ul>
<template id="players-template">
    <p>{{player.name}}</p>
</template>
</body>
Sign up to request clarification or add additional context in comments.

Comments

0

Once you create the Vue app on the body element, you can use its component anywhere within that element. Since you named the component players, you'd render it like this:

<body>
    <players :players="players"></players>
</body>

Then in your Vue

new Vue({
el: 'body',
data: function(){
    return { players:[] }
},
methods: {
    someMethod: function() {
        //var result = ajax result
        this.players = result;
    }
}
});

Since players is a prop on your list component, you pass it in with :players="players". Then if players updates on the parent app, the list component would automatically update according to the new list.

Comments

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.