<div class="li" @click="One()">One</div> One: function () { this.$router.push({ name:"one", params:{ id:"0" } }) }
<router-link class="li" :to="{name:'one',params:{id:'0'}}" tag="div">One</router-link> <router-link class="li" :to="{path:'/two',query:{id:'1'}}" tag="div">Two</router-link> <router-link class="li" :to="{path:'/three',query:{id:'2'}}" tag="div">Three</router-link>
router:new VueRouter({ routes:[ {path:'/myLogin',component:testLogin}, {path:'/myRegister',component:testRegister} ] })
<template> <div id="app"> <transition :name="animate"> <router-view id="view"></router-view> </transition> </div> </template> <script> export default { name: 'App', data () { return { animate: "", } }, watch: { //to爲下個頁面,from爲源路由。經過再路由的配置中將頁面的入棧方式設置爲1.再頁面出棧的時候設置爲2 $route: function(to,from) { /* 0: 不作動畫 1: 左切換 2: 右切換 3: 上切換 4: 下切換 */ let animate = this.$router.animate || to.meta.slide; if(!animate) { this.animate = "" } else { this.animate = animate === 1? "slide-left": animate === 2? "slide-right": animate === 3? "slide-top": animate === 4? "slide-bottom":"" } //當animate爲1的時候頁面想左滑動,2爲右,3爲上,4爲下,0沒有動畫, this.$router.animate = 0;//恢復狀態 // console.log(this.animate); } } } </script> <style> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; font-size: 26.67vw; } .wraper { width: 100%; height: 100%; } .container { width: 100%; height: 100%; display: flex; flex-direction: column; overflow: hidden; background-color: #ffff; } #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; width: 100%; height: 100%; } //不一樣動畫的c3樣式 #view { position: absolute; left: 0; top: 0; width: 100%; height: 100%; transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1); } .slide-left-enter, .slide-right-leave-active { opacity: 0; transform: translate(100%, 0); } .slide-left-leave-active, .slide-right-enter { opacity: 0; transform: translate(-100%, 0); } .slide-top-enter, .slide-bottom-leave-active { opacity: 0; transform: translate(0, 100%); } .slide-top-leave-active, .slide-bottom-enter { opacity: 0; transform: translate(0, -100%); } </style>
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' // 全局路由返回,再頁面返回上級頁面時,能夠經過調用back方法返回上級頁面 Vue.prototype.back = (route) =>{ route.animate = 2;//設置路由返回頁面的動畫方式 window.history.go(-1);//返回一級頁面 } Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
import Vue from 'vue' import Router from 'vue-router' // import HelloWorld from '@/components/HelloWorld' import Home from "@/views/home/index" import One from "@/views/one/index" import Two from "@/views/two/index" import Three from "@/views/three/index" Vue.use(Router); Router.prototype.animate = 0;//給全部的路由設置動畫初始化爲0,無動畫效果 //經過給不一樣的路由設置不一樣的slide來實現頁面不一樣入棧方式爲1,2,3,4 export default new Router({ routes: [ { path: '/', name: '/', component: Home }, { path: "/one", name: "one", meta: { slide: 1, }, component: One, }, { path: "/two", name: "two", meta: { slide: 1, }, component: Two }, { path: "/three", name: "three", meta: { slide: 1, }, component: Three } ] })
home/index.vue <template> <div class="wraper"> <div class="container"> <div class="section"> <h3>這裏是首頁</h3> <h3>編程式跳轉</h3> <div class="main"> <!--編程式路由跳轉--> <div class="li" @click="One()">One</div> <div class="li" @click="Two()">Two</div> <div class="li" @click="Three()">Three</div> </div> <h3>router-link跳轉</h3> <div class="main"> <!--編程式路由跳轉--> <router-link class="li" :to="{name:'one',params:{id:'0'}}" tag="div">One</router-link> <router-link class="li" :to="{path:'/two',query:{id:'1'}}" tag="div">Two</router-link> <router-link class="li" :to="{path:'/three',query:{id:'2'}}" tag="div">Three</router-link> </div> </div> </div> </div> </template> <script> export default { methods: { One: function () { this.$router.push({ name:"one", params:{ id:"0" } }) }, Two: function () { this.$router.push({ path:"/two", query:{ id:"1" } }) }, Three: function () { this.$router.push({ path:"/three", query:{ id:"2" } }) }, } } </script> <style scoped> .section { width: 100%; flex: 1; overflow-y: auto; overflow-x: hidden; } * { font-size: 16px; } .main { width: 100%; height: .5rem; display: flex; align-items: center; border-bottom: 1px solid #ccc; border-top: 1px solid #ccc; } .main div { flex: 1; height: .5rem; line-height: .5rem; } .main div:hover { background-color: #999988; } .main div:nth-of-type(2) { box-sizing: border-box; border-left: 1px solid #ccc; border-right: 1px solid #ccc; } </style>
<template> <div class="wraper"> <div class="container"> <div class="back" @click="back($router)">Back(返回上一級頁面)</div> <h3>這裏One頁面</h3> <h3>首頁傳過來{{msg}}</h3> </div> </div> </template> <script> export default { data () { return { msg: "" } }, methods: { getParams() { this.msg = this.$route.params.id; } }, mounted() { this.getParams(); }, watch: { '$route': 'getParams' } } </script> <style scoped> * { font-size: 16px; } .back { height: .5rem; line-height: .5rem; border-bottom: 1px solid #ccc; background-color: #999; color: #fff; } .back:hover { background-color: #999; } </style>
<template> <div class="wraper"> <div class="container"> <h3>這裏是Two頁面</h3> <h3>首頁傳過來{{msg}}</h3> <div class="back" @click="back($router)">Back(返回上一級頁面)</div> </div> </div> </template> <script> export default { data () { return { msg: "" } }, methods: { getParams() { this.msg = this.$route.query.id; } }, mounted() { this.getParams(); }, watch: { '$route': 'getParams' } } </script> <style scoped> * { font-size: 16px; } .back { width: 100%; height: .5rem; line-height: .5rem; background-color: #ccc; border-bottom: 1px solid #ccc; } </style>
<template> <div class="wraper"> <div class="container"> <h3>這裏是Two頁面</h3> <h3>首頁傳過來{{msg}}</h3> <div class="back" @click="back($router)">Back(返回上一級頁面)</div> </div> </div> </template> <script> export default { data () { return { msg: "" } }, methods: { getParams() { this.msg = this.$route.query.id; } }, mounted() { this.getParams(); }, watch: { '$route': 'getParams' } } </script> <style scoped> * { font-size: 16px; } .back { width: 100%; height: .5rem; line-height: .5rem; background-color: #ccc; border-bottom: 1px solid #ccc; } </style>
個性簽名:海到無邊天做岸,山登絕頂人爲峯!html