2

I'm brand new to and I'm trying to get a simple site set up with it. I have a component for each page (About us, Contact us, etc).

This is my working starting point

Vue.component('about-us', {
    template: '<div>...lots of markup...</div>'
});

I want to transform that code into something that works asynchronously. Attempting to follow the docs, here is my code:

Vue.component('about-us', function(resolve, reject){
    let template_string = '';

    // Load the template with ajax
    $.post(ajax_url, {'action': 'get_about_page'}, function(r){
        r = JSON.parse(r);

        // Save the template string
        if( r.success ) {
            template_string = r.data;
        }
    });

    // Resolve callback for Vue
    resolve({
        template: template_string
    });
});

The error I get is:

[Vue warn]: Failed to mount component: template or render function not defined. found in ---> Anonymous Root

Question: Is my approach wrong? Is it a syntax problem? I'm not sure if I'm on the right path. (yes I have jQuery loaded)

1
  • Put the resolve call in the callback: if (r.success) { resolve({ template: r.data }) } Commented Jul 25, 2017 at 19:36

2 Answers 2

1

You need to move your resolve into the ajax callback.

Vue.component('about-us', function(resolve, reject){
    // Load the template with ajax
    $.post(ajax_url, {'action': 'get_about_page'}, function(r){
        r = JSON.parse(r);

        // Save the template string
        if( r.success ) {
            // Resolve callback for Vue
            resolve({
              template: r.data
            });
        } else {
          reject("Unable to define component!")
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Im using this construction_

Vue.component('about-us', function (resolve, reject) {
  $.post('ajax_url', {'action': 'get_about_page'}, 'html')
   .done(function (data) {
     resolve({template: data})
   })
})

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.