美文网首页Vue.js
vue--异步组件

vue--异步组件

作者: 花拾superzay | 来源:发表于2020-01-09 15:21 被阅读0次

一个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>

相关文章

网友评论

    本文标题:vue--异步组件

    本文链接:https://www.haomeiwen.com/subject/ypbyactx.html