We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
先定义一个外部的xheader.js来声明一个组件
xheader.js
Vue.component("xheader", { template: ` <header :style="{border:'1px solid red'}">头部组件</header> ` })
这个是异步创建script节点实现异步加载.js文件,从而实现异步加载xheader.js文件
script
.js
// 实现异步加载js文件 function load(componentName, path) { return new Promise(function (resolve, reject) { var script = document.createElement('script'); script.src = path; script.async = true; script.onload = function () { var component = Vue.component(componentName); if (component) { resolve(component); } else { reject(); } }; script.onerror = reject; document.body.appendChild(script); }); }
new Vue({ el: "#demo", template: ` <div> <p>{{name}}</p> <xheader></xheader> </div> `, data: { name: "组件" }, components: { xheader: function (resolve, reject) { // 这里可以用异步方法实现异步加载组件 setTimeout(function () { load('xheader', 'xheader.js').then(resolve, reject); }, 3000) } } })
The text was updated successfully, but these errors were encountered:
No branches or pull requests
构建异步组件
先定义一个外部的
xheader.js
来声明一个组件构建异步加载方法
这个是异步创建
script
节点实现异步加载.js
文件,从而实现异步加载xheader.js
文件注册异步组件
参考文档
The text was updated successfully, but these errors were encountered: