Vue异步加载组件 Vue异步加载about组件
小_陈 人气:0想了解Vue异步加载about组件的相关内容吗,小_陈在本文为您仔细讲解Vue异步加载组件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Vue异步加载,Vue异步加载about组件,Vue异步加载组件,下面大家一起来学习吧。
异步加载about组件
about.js
Vue.component('about', {template: '<div>ABOUT PAGE</div>'});
html代码:
<div id="app"> <router-link to="/home">/home</router-link> <router-link to="/about">/about</router-link> <router-view></router-view> </div> <script src="static/js/vue.min.js"></script> <script src="static/js/vue-router.js"></script> <script> 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(); } } document.body.appendChild(script); }); } var router = new VueRouter({ routes: [{ path: '/', redirect: '/home' }, { path: '/home', component: { template: '<div>HOME PAGE</div>' } },{ path: '/about', component: function (resolve, reject) { load('about', 'static/js/business/about.js').then(resolve, reject); } }] }); var app = new Vue({ el: '#app', router: router }); </script>
加载全部内容