三、Vue菜鸟小教程-嵌套路由、二级路由示例
首先写这篇文章之前先声明一下,这篇文章是在空余时间写出来的,后期还会再往里面添加东西或者改动的,如果有哪里不对也欢迎指出。
在学习过程中我都是先阅读一遍官方文档,然后看例子学的,因为个人觉得在做的过程中学习知识是比较快的。下面就遇到了这个问题,$route.parame.id是什么意思呢,在网上查了好多文档,了解到这些东西。下面先看一下程序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>嵌套路由</title> <script src="vue.js" type="text/javascript"></script> <script src="vue-router.js" type="text/javascript"></script> <!--首先引入vue和vue-router.js--> <style type="text/css"> *{padding: 0px;margin: 0px;} li{list-style: none;float: left;margin: 10px 20px;} a{text-decoration: none;} a:hover{cursor: pointer;} .show{width: 300px;height: 300px;background: #ccc;float: left;margin: 20px 0px;} .clear{clear: both;} </style> </head> <body> <!--文档结构--> <div id="box"> <ul> <li><router-link to="/home">首页</router-link></li> <li><router-link to="/news">新闻</router-link></li> <li><router-link to="/personal">个人中心</router-link></li> <div class="clear"></div> </ul> <div class="show"> <router-view></router-view> </div> </div> <template id="home"> <div> <h2>首页内容</h2> </div> </template> <template id="news"> <div> <h2>新闻内容</h2> <p><router-link to="/news/0">详细内容一</router-link></p> <p><router-link to="/news/1">详细内容二</router-link></p> <p><router-link to="/news/2">详细内容三</router-link></p> </div> </template> <template id="personal"> <div> <h2>个人中心</h2> </div> </template> <template id="detailist"> <div> <h2>详细内容</h2> {{$route.params.id}} {{arr[$route.params.id]}} </div> </template> </body> </html> <!--准备模块--> <script type="text/javascript"> var Home = Vue.extend({ template : "#home", }); var News = Vue.extend({ template : "#news", }); var Personal = Vue.extend({ template : "#personal", }); var Detailist = Vue.extend({ template : "#detailist", data : function () { return{ arr:[ "我是娱乐新闻","我是科技新闻","我是经济新闻" ] } }, }); // 分配路径 var routes = [ {path:"/home",component:Home}, {path:"/news",component:News}, {path:"/news/:id",component:Detailist}, {path:"/personal",component:Personal} ]; // 分配路径 var router = new VueRouter({ routes }); var app = new Vue({ el :"#box", data :{}, router, }) router.push("/home") </script>
二级路由
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <title>Document</title> <script type="text/javascript" src="vue.js"></script> <script type="text/javascript" src="vue-router.js"></script> <style type="text/css"> li {list-style-type: none; display: inline-block;} .show {width: 400px; height: 300px; background: #eee;} </style> </head> <body> <div id="box"> <ul> <li><router-link to="/home">home</router-link></li> <li><router-link to="/news">news</router-link></li> </ul> <div class="show"> <router-view></router-view> </div> </div> <template id="h"> <div> <h2>首页</h2> </div> </template> <template id="n"> <div> <p>新闻列表</p> <p><router-link to="/news/today">今日头条</router-link></p> <p><router-link to="/news/star">娱乐八卦(关爱八卦成长协会)</router-link></p> <p><router-link to="/news/tiyu">体育新闻</router-link></p> <router-view></router-view> </div> </template> </body> </html> <script type="text/javascript"> // 准备模块 var Home = Vue.extend({ template: "#h" }); var NewsList = Vue.extend({ template: "#n" }); // 配置路径 var routes = [ {path:"/home", component:Home}, {path:"/news", component:NewsList, children: [ // 二级路由路径,不加/ {path:"today", component:{ template: "<p>就凭你的胸,你今天也该过节</p>" }}, {path:"star", component:{ template: "<p>某某小鲜肉和某某小花又约会了</p>" }}, {path:"tiyu", component:{ template: "<p>中国足球居然赢了</p>" }}, ] }, ]; // 创建路由实例 var router = new VueRouter({ routes }); var vm = new Vue({ el: "#box", data: { }, // 添加路由 router }); // 路由重定向 router.push("/home"); </script>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: js里面怎么动态的为对象添加属性
- 下一篇: 小icon与后面文字对齐