路由

作者: 张鑫冲 | 来源:发表于2018-09-24 16:50 被阅读0次
    vue的核心插件
    1,根据不同的url访问不同的页面
    2, 创建单页面SPA(SINGLE PAGE APPLICATION)应用

    1,路由的跳转:

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                .router-link-exact-active {
                    color: red;
                }
            </style>
        </head>
        <body>
            <script src="js/vue.js"></script>
            <script src="js/vue-router.js"></script>
            <div id="box">
                <router-link to='/home'>首页</router-link>
                <router-link to='/home1'>首页1</router-link>
                <router-view></router-view>
            </div>
            <script>
                var Home = {
                    template: `
                    <h1>home</h1>
                    `
                }
    
                var Home1 = {
                    template: `
                    <div>
                    <h1>home1</h1>
                    <ul>
                    <li>
                    <router-link to='/Home1/one'>登录</router-link>
                    </li>
                    <li>
                    <router-link to='/Home1/two'>注册</router-link>
                    </li>
                    </ul>
                    <router-view></router-view>
                    </div>
                    `
                }
    
                var One = {
                    template: `
                    <h3>登录</h3>
                    `
                }
                var Two = {
                    template: `
                     <h3>注册</h3>
                    `
                }
                const routes = [{
                        path: '/',
                        component: Home
                    },
                    {
                        path: '/home',
                        component: Home
                    },
                    {
                        path: '/home1',
                        component: Home1,
                        children: [{
                                path: 'one',
                                component: One
                            },
                            {
                                path: 'two',
                                component: Two
                            }
                        ]
                    }
                ]
    
                const router = new VueRouter({
                    routes: routes,
    
                })
    
                new Vue({
                    el: '#box',
                    router: router
                })
            </script>
        </body>
    
    </html>
    
    效果图:
    QQ拼音截图未命名.png
    路由的嵌套:
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
    
        <body>
            <script src="js/vue.js"></script>
            <script src="js/vue-router.js"></script>
            <div id="box">
                <router-link to='/home'>首页</router-link>
                <router-link to='/home1'>首页1</router-link>
                <router-view></router-view>
            </div>
        </body>
        <script>
            var Home = {
                template: `
                <h1>home</h1>
                `
            }
            var Home1 = {
                template: `
              <div>
                <h1>home1</h1>
                 <ul>
                 <li>
                   <router-link to='/home1/one'>登录</router-link>
                  </li>
                   <li>
                  <router-link to='/Home1/two'>注册</router-link>
                  </li>
                 </ul>
                 <router-view></router-view>
                  </div>
                  `
            }
            var One = {
                template: `
            <h2>这是登录<h2>      
                  `
            }
    
            var Two = {
                template: `
                  <h2>这是注册</h2>
                  `
            }
            const routes = [
                //          components
                {
                    path: '/',
                    component: Home
                }
    
                , {
                    path: '/home',
                    component: Home
                }, {
                    path: '/home1',
                    component: Home1,
                    children:[
                    {path:'one',component:One},
                    {path:'two',component:Two},
                    ]
                }
            ]
            const router = new VueRouter({
                routes: routes
            })
            new Vue({
                el: '#box',
                router: router
            })
        </script>
    
    </html>
    
    效果图:
    QQ拼音截图未命名.png

    相关文章

      网友评论

          本文标题:路由

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