javascript - VueJS - How to initialize a template dynamically with the result of an ajax call - Stack Overflow

I want to load the template for a VueJS ponent dynamically.I'd like to make an AJAX call using j

I want to load the template for a VueJS ponent dynamically. I'd like to make an AJAX call using jQuery, and whatever the server returns should be the template of the VueJS ponent. Here's a simplified version of the code with the AJAX call removed since it's irrelevant where the data is ing 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?

I want to load the template for a VueJS ponent dynamically. I'd like to make an AJAX call using jQuery, and whatever the server returns should be the template of the VueJS ponent. Here's a simplified version of the code with the AJAX call removed since it's irrelevant where the data is ing 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?

Share Improve this question edited Jan 27, 2015 at 13:56 nils 27.3k6 gold badges73 silver badges81 bronze badges asked Jan 16, 2015 at 16:01 NightwolfNightwolf 4,6892 gold badges23 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You could use v-partial in your template. And when you've loaded the partial, you can register it via Vue.partial(). The {{ partial }} value is then replaced, thus rendering the new partial.

BoardFeed = Vue.extend
    template: '<div v-partial="{{ partial }}">This should be replaced</div>'
    partials: {"beforeLoad": "<div>This should be replaced</div>"}
    data: ->
            return {items: null, partial: "beforeLoad"}
    created: ->
        Vue.partial("afterLoad", "<div>Template returned from server, what I really want</div>")
        @partial = "afterLoad"

(and excuse any coffee-script errors, I'm not very familiar with it)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744886532a4599154.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信