路由

2018-09-24  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读