ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] #### 1.定义一下单页面路由 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-link to="/hdphp">HDPHP</router-link> <router-link to="/hdcms">HDCMS</router-link> <router-view></router-view> </div> <script> const hdphp={ template:'<h1>hdphp</h1>' } const hdcms={ template:'<h1>hdcms</h1>' } let routes=[ {path:'/hdphp',component:hdphp}, {path:'/hdcms',component:hdcms}, ]; //要把组件交给路由器 let router = new VueRouter({routes:routes}); new Vue({ el: '#hdcms', router:router }); </script> #### 2.vue-router之路由参数的随意设置与伪静态链接地址处理 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-link to="/content">内容 </router-link> <router-view></router-view> </div> <script type="text/x-template" id="content"> <div> cid:{{$route.params.cid}} - id:{{$route.params.id}} <br> <button @click="show">检测参数</button> </div> </script> <script> const content={ template:'#content', methods:{ show(){ console.log(this.$route.params); } } } let routes=[ {path:'/content-:cid-show-:id.html',component:content}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 3.vue-router之路由参数的验证处理保存路由安全 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-link to="/content">内容 </router-link> <router-view></router-view> </div> <script type="text/x-template" id="content"> <div> id:{{$route.params.id}} <br> <button @click="show">检测参数</button> </div> </script> <script> const content={ template:'#content', methods:{ show(){ console.log(this.$route.params); } } } let routes=[ {path:'/content/:id(\\d{2})',component:content}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 4.vue-router之路由参数默认值的设置技巧 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-link to="/content">内容</router-link> <router-view></router-view> </div> <script type="text/x-template" id="content"> <div> id:{{id}} <button @click="show">检测参数</button> </div> </script> <script> const content = { template: '#content', data(){ return { id: 0 } }, mounted(){ this.id = this.$route.params.id; if (!this.id) { this.id = 1; } }, methods: { show(){ console.log(this.$route.params); } } } let routes = [ {path: '/content/:id?', component: content}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 5.vue-router之实例操作新闻列表单页面应用与路由别名的使用 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <router-link :to="{name:'content',params:{id:v.id}}">{{v.title}}</router-link> </li> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <router-link to="/">返回首页</router-link> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'} ]; const home = { template: '#home', data(){ return { news:data } } } const content = { template: '#content', data(){ return { field:{} } }, mounted(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } } } let routes = [ {path: '/', component: home}, {path: '/content/:id', component: content,name:'content'}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 6.Vue-router之路由嵌套在文章系统中的使用方法 注意:需要引入 Vue-roter.js <div id="hdcms"> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <router-link :to="{name:'content',params:{id:v.id}}">{{v.title}}</router-link> </li> <router-view></router-view> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <router-link to="/">返回首页</router-link> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'} ]; const home = { template: '#home', data(){ return { news:data } } } const content = { template: '#content', data(){ return { field:{} } }, mounted(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } } } let routes = [ {path: '/', component: home,children:[ {path: '/content/:id', component: content,name:'content'}, ]}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 7.Vue-router之使用watch与mounted解决同一组件页面不刷新数据的问题 <div id="hdcms"> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <router-link :to="{name:'content',params:{id:v.id}}">{{v.title}}</router-link> </li> <router-view></router-view> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <router-link to="/">返回首页</router-link> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'} ]; const home = { template: '#home', data(){ return { news:data } } } const content = { template: '#content', data(){ return { field:{} } }, watch:{ '$route'(to,from){ this.load(); } }, mounted(){ this.load(); }, methods:{ load(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } } } } let routes = [ {path: '/', component: home,children:[ {path: '/content/:id', component: content,name:'content'}, ]}, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 8.vue-router之通过程序控制路由跳转 <div id="hdcms"> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <a href="" @click.prevent="go(v.id)">{{v.title}}</a> </li> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <a href="" @click.prevent="back()">返回</a> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'} ]; const home = { template: '#home', data(){ return { news:data } }, methods:{ go(id){ //写业务逻辑 // var url = '/content/'+id; //{name:'content',params:{id:id}} var url ={path:'/content/'+id} this.$router.push(url); } } } const content = { template: '#content', data(){ return { field:{} } }, watch:{ '$route'(to,from){ this.load(); } }, mounted(){ this.load(); }, methods:{ load(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } }, back(){ this.$router.go(-1); } } } let routes = [ {path: '/', component: home}, {path: '/content/:id', component: content,name:'content'} ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 9.vue-router之命名视图的实例讲解 <style> .menu{border:solid 2px #999;padding:10px;display: block;} .news{float:left;border:solid 1px red;padding:20px;width:300px;} .slide{float:left;border:solid 1px red;padding:20px;width:300px;} </style> <div id="hdcms"> <router-view class="menu"></router-view> <router-view class="news" name="news"></router-view> <router-view class="slide" name="slide"></router-view> </div> <script type="text/x-template" id="menu"> <div> <a href="http://houdunren.com">后盾人</a> <a href="http://www.houdunwang.com">后盾IT教育</a> </div> </script> <script type="text/x-template" id="news"> <div> <li v-for="v in news">{{v.title}}</li> </div> </script> <script type="text/x-template" id="slide"> <div> <li v-for="v in data">{{v.title}}</li> </div> </script> <script> const menu = { template: '#menu', } const news = { template: '#news', data(){ return { news:[ {title:'houdunren.com'}, {title:'hdphp.com'}, ] } } } const slide = { template: '#slide', data(){ return { data:[ {title:'后盾人'}, {title:'后盾PHP培训'}, ] } } } let routes = [ { path: '/', components: { default: menu, news: news, slide: slide } }, ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 10.vue-router之重定向redirect的使用技巧 <div id="hdcms"> <router-link to="/">首页</router-link> <a href="http://www.houdunwang.com">后盾人</a> <router-link to="/about">关于我们</router-link> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <a href="" @click.prevent="go(v.id)">{{v.title}}</a> </li> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <a href="" @click.prevent="back()">返回</a> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'}, {id:3,title:'关于我们',content:'一个认真录制视频教程的有激情的团队!!!!'} ]; const home = { template: '#home', data(){ return { news:data } }, methods:{ go(id){ //写业务逻辑 // var url = '/content/'+id; //{name:'content',params:{id:id}} var url ={path:'/content/'+id} this.$router.push(url); } } } const content = { template: '#content', data(){ return { field:{} } }, watch:{ '$route'(to,from){ this.load(); } }, mounted(){ this.load(); }, methods:{ load(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } }, back(){ this.$router.go(-1); } } } let routes = [ {path: '/', component: home}, {path: '/content/:id', component: content,name:'content'}, {path: '/about', redirect:{name:'content',params:{id:3}}} ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 11.vue-router之使用路由别名定制URL <div id="hdcms"> <router-link to="/">首页</router-link> <a href="http://www.houdunwang.com">后盾人</a> <router-link to="/about">关于我们</router-link> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <a href="" @click.prevent="go(v.id)">{{v.title}}</a> </li> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <a href="" @click.prevent="back()">返回</a> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'}, {id:3,title:'关于我们',content:'一个认真录制视频教程的有激情的团队!!!!'} ]; const home = { template: '#home', data(){ return { news:data } }, methods:{ go(id){ //写业务逻辑 // var url = '/content/'+id; //{name:'content',params:{id:id}} var url ={path:'/content/'+id} this.$router.push(url); } } } const content = { template: '#content', data(){ return { field:{} } }, watch:{ '$route'(to,from){ this.load(); } }, mounted(){ this.load(); }, methods:{ load(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } }, back(){ this.$router.go(-1); } } } let routes = [ {path: '/', component: home}, {path: '/content/:id', component: content,name:'content'}, {path: '/content/3', alias:['/about']}, {path: '/content/2', alias:['/hdphp']} ]; //要把组件交给路由器 let router = new VueRouter({routes}); new Vue({ el: '#hdcms', router }); </script> #### 12.vue-router之使用html5history模式去除hash锚点生成优雅的url 1.创建..htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [L] <div id="hdcms"> <router-view></router-view> </div> <script type="text/x-template" id="home"> <div> <li v-for="v in news"> <a href="" @click.prevent="go(v.id)">{{v.title}}</a> </li> </div> </script> <script type="text/x-template" id="content"> <div> <h1>{{field.title}}-{{field.id}}</h1> <p> {{field.content}} </p> <a href="" @click.prevent="back()">返回</a> </div> </script> <script> var data = [ {id:1,title:'HDPHP开源免费框架',content:'这是内容....'}, {id:2,title:'HDCMS内容管理系统',content:'这是HDCMS内容....'} ]; const home = { template: '#home', data(){ return { news:data } }, methods:{ go(id){ //写业务逻辑 // var url = '/content/'+id; //{name:'content',params:{id:id}} var url ={path:'/content/'+id+'.html'} this.$router.push(url); } } } const NotFound = { template: '<h1>你请求的页面不存在</h1>', } const content = { template: '#content', data(){ return { field:{} } }, watch:{ '$route'(to,from){ this.load(); } }, mounted(){ this.load(); }, methods:{ load(){ var id =this.$route.params.id; for(let k=0;k<data.length;k++){ if(data[k].id==id){ this.field = data[k]; } } }, back(){ this.$router.go(-1); } } } let routes = [ {path: '/', component: home}, {path: '/content/:id.html', component: content,name:'content'}, {path: '*', component: NotFound}, ]; //要把组件交给路由器 let router = new VueRouter({ mode:'history', routes }); new Vue({ el: '#hdcms', router }); </script>