一个demo说明什么是异步组件,拷贝到本地运行查看效果
<!DOCTYPE html>
<html>
<head>
<title>异步组件</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
</head>
<body>
<div id='app'>
<router-view></router-view>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
//异步组件定义
function foo() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ //组件对象
template: '<div>当前页面:小cccccccc</div>',
name: 'ccc',
})
}, 5000)
})
}
//配置路由
let router = new VueRouter({
mode: 'hash',
routes: [{
path: '/ccc',
component: foo,
}]
});
//执行vue
let model = new Vue({
el: "#app",
data: {},
created() {
this.$router.push('/ccc');
},
router: router,
});
</script>
</html>
网友评论