我是vuejs的新手,正在尝试建立一个简单的网站。每个页面都有一个组件(关于我们,联系我们等)。
这是我的工作起点
Vue.component('about-us', {
template: '<div>...lots of markup...</div>'
});
我想将该代码转换为异步工作的代码。尝试关注文档,这是我的代码:
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
});
});
我得到的错误是:
[Vue警告]:无法装入组件:模板或渲染函数未定义。在--->匿名根目录中找到
问题:我的方法错误吗?这是语法问题吗?我不确定自己走的路是否正确。(是的,我已经加载了jQuery)
您需要将您的解决方案移至ajax回调中。
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!")
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句