如果我理解正確,您會從您的AJAX調用中獲取自定義組件的標籤,並且想要構建一個Vue組件。
所以我們可以說這是你的<test-component>
:
Vue.component('test-component', {
template: "<p>I am the test component template</p>",
methods: {
// Component logic...
}
});
現在,在您的應用程序的某個地方,你讓AJAX調用:
$(document).ready(function() {
var html = '<test-component></test-component>';
var url = "https://jsonplaceholder.typicode.com/posts";
$.get(url, function (data) {
var res = Vue.compile(html)
new Vue({
render: res.render,
staticRenderFns: res.staticRenderFns
}).$mount('#media_lib_app')
}.bind(this));
})
你的元件安裝點:
<div id="media_lib_app"></div>
更多關於.compile:
https://vuejs.org/v2/api/#Vue-compile
注意:Vue.compile()
僅在完整版本中可用。
你可以在這裏找到一個工作示例:
https://jsbin.com/motuvokeha/edit?html,js,output
希望這可以幫助你:)
你能告訴你的代碼? – thanksd
實際的代碼太大,因爲它是一個依賴於jQuery的舊項目,但這是在後 –
中解釋的確切情況如果您可以提供[最小化,完整和可驗證](https://stackoverflow.com/help/mcve)您的問題的例子。 – thanksd