vue2.0开发笔记

vue-router及嵌套路由注意事项

2017-10-18  本文已影响10人  iqing2012
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.js"></script>
    <script src="./vue-router.js"></script>

</head>

<body>
    <div id="app">
           <router-link to="/home">home</router-link>
           <router-link to="/about">about</router-link>
        <router-view></router-view>
    </div>
</body>
<script>
    var router=new VueRouter({
        routes:[{
            name:'home',path:'/home',
            component:{template:'<div>home{{$route.params.id}}' +
                    '   <router-link to="/home/profile">登录</router-link>'+
            '<router-view></router-view>' +
            '</div>'},
//这里不要写乱了。要写在一个单路由下面
            children:[{path:'/home/profile',component:{template:'<div>profile</div>'}}]

        },{
            name:'about',path:'/about',component:{template:'<div>about</div>'}
        },{
            name:'index',path:'/',redirect:'/about'
        }]
    })
   var app=new Vue({
       el:'#app',
       router:router,
       mounted:function () {
           this.getparam();
       },
       methods:{
           getparam(){

//这里是$route取得参数。
               console.log(this.$route.params.id);
           }
       }

   })


</script>

</html>
上一篇下一篇

猜你喜欢

热点阅读