首页 > 技术文章 > vue2.0路由数据嵌套

webarn 2017-03-01 18:49 原文

  <script>
        //组件
        const Home = {
            template:'<h3>我是主页</h3>'
        };
        const Users = {
            template:`
            <div>
                <h3>我是用户信息</h3>
                <ul>
                    <li><router-link to="/users/Amumu/age/15">阿木木</router-link></li>
                    <li><router-link to="/users/Ezreal/age/23">伊泽瑞尔</router-link></li>
                    <li><router-link to="/users/Annie/age/12">安妮</router-link></li>
                    <li><router-link to="/users/Janna/age/26">迦娜</router-link></li>
                    <li><router-link to="/users/Katarina/age/27">卡特琳娜</router-link></li>
                </ul>
                <div>
                    <router-view></router-view>        
                </div>    
            </div>
            
            `
        }
        const About = {
            template:'<h3>我是关于</h3>'
        }
        const Teemo={
            template:'<div>{{$route.params}}</div>'
        }
        //配置路由
        const routes = [ 
            {path:'/home', component :Home},
            {
                path:'/users', 
                component:Users,
                children:[
                    {path:':users/age/:age',component:Teemo}
                ]
            },
            {path:'/about',component:About},
            //重定向
            {path:'*',redirect:'/home'}
        ]
        //生成路由实例
        const router = new VueRouter({
            routes
        })
        //挂载到vue上
        new Vue({
            router,
            el:'#box'
        })
    </script>

 

推荐阅读