是啊,這是相當簡單的實際上是:
const Vue = require('vue');
const vueInstance = new Vue({
// don't specify the 'el' prop there
});
doingAnAjaxCall() // assuming it returns a promise
.then(() => {
vueInstance.$mount('#root'); // only now the vue instance is mounted
});
如果Vue的情況下沒有得到實例化的EL選項,這將是「未安裝」狀態,沒有相關聯的DOM元素。 vm。$ mount()可用於手動啓動未掛載的Vue實例的掛載。
參見:https://vuejs.org/v2/api/#vm-mount
因此,對於你的情況,你可以使用任何異步機制來檢測AJAX調用結束。也許最簡單的解決方案是將回調函數傳遞給你的插件對象,並將你的vue實例掛載進去。
/* Define plugin */
MyPlugin = {};
MyPlugin.install = function(Vue, options) {
doingAnAjaxCall()
.then(data => {
// do something with data
options.callback();
});
};
const Vue = require('vue');
/* Create the vue instance */
const vueInstance = new Vue({
// don't specify the 'el' prop there
});
/* Install the plugin */
Vue.use(MyPlugin, { // This object will be passed as options to the plugin install()
callback:() => {
vueInstance.$mount('#root'); // only now the vue instance is mounted
}
});
謝謝..我會試試這個。在我看來很好。 –