question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to initialize a template dynamically with the result of an ajax call

See original GitHub issue

I want to load the template for a VueJS component dynamically. I’d like to make an AJAX call using jQuery, and whatever the server returns should be the template of the VueJS component. Here’s a simplified version of the code with the AJAX call removed since it’s irrelevant where the data is coming from:

BoardFeed = Vue.extend
    template: '<div>This should be replaced</div>'
    data: ->
            return items: null
    created: ->
        @template = "<div>Template returned from server, what I really want</div>"

In the above example I’m using the created hook which I thought would be suitable for this, but the newer template is never rendered, only the older one.

Is it possible to achieve this?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
multimericcommented, Jan 17, 2015

I still think that’s the same situation. Have a look at this fiddle: (code is duplicated below):

<div v-component="{{currentView}}"></div>
//Create the 'default' component
Vue.component("default", {
    template: '<div>This should be replaced (and will be in 2 seconds)</div>'
});

//Create the parent ViewModel
var vm = new Vue({
    el: "body",
    data: {
        currentView: "default"
    }
});

//Pretend to load the data from the server
//This would actually be $.get("/url", data, function(){...});
window.setTimeout(function () {

    //Create the new component using the template we received
    Vue.component("BoardFeed", {
        template: '<div>Template returned from server, what I really want</div><br>And directives work too:<div v-repeat="items">{{$value}}</div>',
        data: function () {
            return {
                items: [1, 2, 3]
            };
        }
    });

    //And then change the page to that component
    vm.currentView = "BoardFeed";
}, 2000);
12reactions
emahunicommented, Oct 29, 2016

@TMiguelT Ok go it!, so how do you do it for single file template syntax returned by ajax like so:


<template>
    <h1 class="bg-success">{{name}}</h1>
    <div>this is the template that was loaded through ajax</div>
</template>

<style lang="sass" rel="stylesheet/css">

</style>

<script>
    export default {
        data () {
            return  {
                name:'ajax template'
            }
        }
    }
</script>

how do i make that to work with vue… I think putting a native protected runtime template compiler is something that Vue is missing and i want it to work this way coz of ide support etc. I don’t want to use webpack code splitting and the like… I want to be able to load a template through Ajax and use it in the browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VueJS - How to initialize a template dynamically with the result ...
I want to load the template for a VueJS component dynamically. I'd like to make an AJAX call using jQuery, and whatever the...
Read more >
Javascript – VueJS – How to initialize a template dynamically ...
I want to load the template for a VueJS component dynamically. I'd like to make an AJAX call using jQuery, and whatever the...
Read more >
VueJS - How to initialize a template dynamically with the result ...
Coding example for the question VueJS - How to initialize a template dynamically with the result of an ajax call-Vue.js.
Read more >
Magento 2: Set Template Dynamically Using Ajax Call
1 Answer 1 · use below code in to button click · app\etc\vender\module\Controller\Ajax\Ajax.php · app\etc\vender\module\view\frontend\layout\ ...
Read more >
Getting started - Developer guides - MDN Web Docs
In this article. What's AJAX? Step 1 – How to make an HTTP request; Step 2 – Handling the server response; Step 3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found