Home > Web Front-end > JS Tutorial > body text

How to return to the initial position of the original page after page jump in vue

亚连
Release: 2018-06-06 10:57:02
Original
5158 people have browsed it

Below I will share with you a method of returning to the initial position of the original page after a vue page jump. It has a good reference value and I hope it will be helpful to everyone.

After the vue page jumps to a new page, and then returns from the new page to the original page, if you want to return to the initial position of the original page, how to solve this problem? First of all, we should record the scrollY that jumped out when jumping out of the page. When returning to the original page, just set the return position to the recorded scrolly. I use the vuex state manager to save the scrolly. The entire environment is built based on vue-cli

1. Configure vuex in main.js

//引用vuex 
import Vuex from 'vuex' 
Vue.use(Vuex)
Copy after login

2. vuex status management in main.js

var store = new Vuex.Store({ 
 state: { 
 recruitScrollY:0 
 }, 
 getters: { 
 recruitScrollY:state => state.recruitScrollY 
 }, 
 mutations: { 
 changeRecruitScrollY(state,recruitScrollY) { 
 state.recruitScrollY = recruitScrollY 
 } 
 }, 
 actions: { 
 
 }, 
 modules: {} 
})
Copy after login

Here is a listview page and details page, listview The page is the original page. The listview page jumps to the details page, and then returns to the position before jumping to the details page. Write code on the listview page

beforeRouteLeave(to, from, next) { 
 let position = window.scrollY //记录离开页面的位置 
 if (position == null) position = 0 
 this.$store.commit('changeRecruitScrollY', position) //离开路由时把位置存起来 
 next() 
 }, 
 watch: { 
 '$route' (to, from) { 
 if (to.name === 'NewRecruit') {//跳转的的页面的名称是"NewRecruit",这里就相当于我们listview页面,或者原始页面 
 let recruitScrollY = this.$store.state.recruitScrollY 
 window.scroll(0, recruitScrollY) 
 } 
 } 
 }
Copy after login

4. If you want to avoid the execution of the created life cycle, you can use cache keepAlive. I will also share it here.

(1) App.vue template

<keep-alive v-if="$route.meta.keepAlive"> 
 <router-view></router-view> 
 </keep-alive> 
 <router-view v-if="!$route.meta.keepAlive"></router-view>
Copy after login

(2)router index.js

Vue.use(Router) 
 
const routerApp = new Router({ 
 routes: [{ 
 { 
 path: &#39;/NewRecruit&#39;, 
 name: &#39;NewRecruit&#39;, 
 component: NewRecruit, 
 meta: { 
 keepAlive: true 
 } 
 }, 
 { 
 path: &#39;/NewRecruitDesc/:id&#39;, 
 name: &#39;NewRecruitDesc&#39;, 
 component: NewRecruitDesc, 
 meta: { 
 keepAlive: true 
 } 
 }, 
 { 
 path: &#39;/SubmitSucess&#39;, 
 name: &#39;SubmitSucess&#39;, 
 component: SubmitSucess, 
 meta: { 
 keepAlive: false 
 } 
 } 
 ] 
}) 
 
export default routerApp
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Implement date selection component in vue mobile terminal

Use node to implement built-in debugging

react webpack packaged files (detailed tutorial)

The above is the detailed content of How to return to the initial position of the original page after page jump in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template