PHPer快速上手VUE——边学边练5–利用路由进行页面跳转
看了不少有关vue路由的文档,基本上都是在单页面操作。很少有系统性的路由例子。所以上一篇我把路由的生产状态的如何使用的代码贴了出来。vue路由的功能很多,我只是把常用的功能写了出来。
这一篇,我们先回想一下,我之前文章里,说要做一个头部和尾部的公用组件,我直接把头部的代码贴出来,因为它也涉及到了路由的跳转。
我们都知道,大部分手机端页面,最上部分,有个 < 回退,点击后回退到上一页,头部组件里,就有这部分代码,大家仔细看一下,代码很简单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<template> <header class="ui-header ui-header-positive ui-border-b"> <div class="ui-row-flex ui-whitespace"> <div class="ui-col ui-col-1 left-item"><img src="../assets/images/[email protected]" v-on:click="goBack"></div> <div class="ui-col ui-col-3"><h1>{{ $route.meta.title }} </h1></div> <div class="ui-col ui-col-1"></div> </div> </header> </template> <script> export default { name: 'Header', data: function () { return {} }, methods: { goBack() { if (window.history.length > 1) { this.$router.back(); } else { this.$router.push('home'); } }, } } </script> <style scoped> .ui-row-flex .left-item { display: flex; flex-direction: row; justify-content: flex-start; align-items: center; vertical-align: middle; } .ui-row-flex .right-item { display: flex; flex-direction: row; justify-content: flex-end; align-items: center; margin-right: 15px; } .ui-row-flex h1 { align-items: center; vertical-align: middle; text-align: center; margin-top: 3px; margin-left: 15px; } </style> |
- $route.meta.title ,就是每个页面路由中定义的title标题
- this.$router.back() 返回上一页 ,还有个 this.$router.go(-1)也是有类似的效果
- this.$router.push(‘home’) 将路由定义到路由表name = home的页面
同样,首页的九宫格代码里也包含了跳转:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<template> <div> <gk-tips type="warn" linkType="link" :btnLink="['button','http://www.baidu.com']">自定义组件测试</gk-tips> <div class="ui-feeds"> <ul> <li v-for="n in numbers"> <span v-bind:style="backImg(n)" v-on:click="gotoPage(n)"></span> </li> </ul> </div> </div> </template> <script> import Tips from "@/components/Tips.vue"; export default { name: 'Home', data () { return { numbers: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], path: ['/OrderList','/OrderDetail'], } }, methods: { backImg: function (n) { return 'background-image:url("'+require('@/assets/images/jiu0' + n + '.png')+'")' }, gotoPage(n) { let i = n - 1; this.$router.push(this.path[i]); }, }, components: { 'gk-tips' : Tips } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style> |
我只做了两个页面,所以,只放了这两个页面在数组里。。
还有一个需要提醒大家的:
使用我这种方式的路由表,url会出现#,如何取消?
还有一个,就是如何指定app的首页
至此,大家就完成了vue的初始入门,接下来还有页面状态,参数传递,数据状态值存储等等,那些编程类的东西,对于phper应该不在话下了。
api的代码我没有贴出,其实axio类似ajax,不过要需要学习es6语法。
路由的一些小资料:https://www.cnblogs.com/yadi001/p/12844872.html
噢!评论已关闭。