页面切换到默认显示顶部

方法一

使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样。

vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动。

在路由配置中使用scrollBehavior

    scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0 }
}
}

如下例子, 使得每次进入页面都在页面顶部。

import Vue from 'vue'
import Router from 'vue-router'
import {wechatAuth,wechatOauth} from '@/utils/auth.js'
import store from '@/store' Vue.use(Router) const routes = {
mode: 'hash',
routes: [
{ path: '/', redirect: '/introduce' },
{
path: '/introduce',
name:'introduce',
component: () => import('@/pages/introduce')
},
{
path: '/register',
component: () => import('@/pages/register')
},
/* {
path: '/auth',
component: () => import('@/pages/auth')
}, */
{
path: '/businessCard',
component: () => import('@/pages/businessCard')
},
{
path: '/category',
component: () => import('@/pages/category')
},
{
path: '/index',
name:'index',
component: () => import('@/pages/index')
},
{
path: '/orderList',
name:'orderList',
component: () => import('@/pages/orderList')
},
{
path: '/orderDetail',
name:'orderDetail',
component: () => import('@/pages/orderDetail')
},
{
path: '/acceptOrder',
name:'acceptOrder',
component: () => import('@/pages/acceptOrder')
},
{
path: '/feedBackDetail',
name:'feedBackDetail',
component: () => import('@/pages/feedBackDetail')
},
],
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0
}
}
}

} const route = new Router(
routes
) route.beforeEach((to, from, next) => { //获取当前页面链接进入路由签名
let link = route.resolve({
path: window.location.href
}) //判断有没有code
if(link.href.includes('/?code')){ let {code} = link.route.query
let uri = to.fullPath
wechatOauth(code,uri) }else{ //没有code再进入判断有没有用户资料
if(store.state.userInfo.userId === null){ //用户数据初始化请求
store.dispatch('getUserData').then(res => {
//这里是已授权
//已授权进入正常的判断跳转流程 //获取用户id及绑定手机号码
let {userId,regTel} = res if(userId == -1){
//判断用户是否关注了公众号
next('/introduce')
}else{
//判断有没有绑定手机号码
if(regTel == ''){
next('/register')
}else{
//这里是请求到用户资料进入的正常流程
next()
}
} }).catch(err => {
//这里是未授权
//未授权就请求微信接口进行授权
wechatAuth(to.fullPath)
})
}else{
//这里是没有问题进入的正常流程
next()
}
}
}) export default route

方法二:vue里面写法如下,至于updated生命周期里面

    updated() {
window.scroll(0, 0);
}

方法三:router拦截控制

//路由跳转后,页面回到顶部
router.afterEach(() => {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
})

https://www.cnblogs.com/sophie_wang/p/7880261.html

https://blog.csdn.net/u013144287/article/details/78985551

https://blog.csdn.net/csl125/article/details/83996314

vue项目页面切换到默认显示顶部的更多相关文章

  1. vue项目页面空白

    vue项目页面空白 今天新建项目,然后发现路由也改了 app.vue里面也是啥都没有, 但是访问http://localhost:8080/#/login 能访问 里面确实空白的 错误: 错误原因: ...

  2. Vue项目页面跳转时候的,浏览器窗口上方的进度条显示

    1.安装: cnpm install --save nprogress 2.在main.js中引入: import NProgress from 'nprogress' import 'nprogre ...

  3. 97、进入ScrollView根布局页面,直接跳到页面底部,不能显示顶部内容

    API使用:http://www.cnblogs.com/over140/archive/2011/01/27/1945964.html 以ScrollView为根的部局,不能从顶部显示其包含的页面内 ...

  4. 基于Vue的页面切换左右滑动效果

    HTML文本页面: <template> <div id="app> <transition :name="direction" mode= ...

  5. Vue 动态控制页面中按钮是否显示和样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vue 项目路由跳转后显示不同的title

    1.在router/index.js的每个路由中配置title 2.在项目中运行命令 npm install vue-wechat-title --save 安装插件(在 package.json文件 ...

  7. IIS部署vue项目页面刷新404,url重写问题解决办法

    这里需要用到URL重写工具 --URL Rewrite(默认没有,需要自己下载安装) 如果IIS上默认有安装Web平台安装程序,我们可以使用平台自动安装URL Rewrite重写工具,打开IIS在管理 ...

  8. 【Vue中的坑】vue项目中动态绑定src不显示图片解决方法

    v-for绑定src的数据如下: data() { return { img_src:"../../assets/images/mirror-service.png" } } 渲染 ...

  9. vue项目build后font-awesome不显示问题

    解决办法: 修改build目录下的utils.js:添加 publicPath: '../../' // Extract CSS when that option is specified // (w ...

随机推荐

  1. canvas模拟中国铁路运行图

    原理说明 1.在知道canvas画布尺寸的情况下,需要将地理经纬度信息转换为canvas画布x,y坐标,因为中国地图地理经纬度坐标取值范围为73.33-135.05(经度)37-50(维度),所以第一 ...

  2. JUC - ReentrantLock 的基本用法 以及 lock()、tryLock()、lockInterruptibly()的区别

    ReentrantLock 与 synchronized对比 最近有在阅读Java并发编程实战这本书,又看到了ReentrantLock和synchronized的对比,发现自己以前对于Renntra ...

  3. Redis备忘(一)

    hash: 渐进式rehash:同时查询新旧两个hash,然后在后续定时任务以及hash的子指令中,循序渐进将旧的迁移到新的hash表 Redis应用: 1.分布式锁: 实现1:setnx+expir ...

  4. Redis(十五)Redis 的一些常用技术(Spring 环境下)

    一.Redis 事务与锁机制 1.Redis的基础事务 在Redis中开启事务的命令是 multi 命令, 而执行事务的命令是 exec 命令.multi 到 exec 命令之间的 Redis 命令将 ...

  5. kali2019里安装Burp Suite安装破解版加汉化版

    Burpsuite是一个强大web漏洞挖掘工具,截断代理,解码和编码,Fuzzy进行各种注入和暴力破解 插件扩展,有多个模块 Burp Suite没有中文版的,我英语又不好,我虽然精通Burp Sui ...

  6. Mycat分布式数据库架构解决方案--配置defaultAccount属性报错解决方案

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 该文章 ...

  7. phpStorm //todo 的用途

    用phpstorm看到别人的代码使用了注释//todo,且todo是彩色的 我想这个应该是有点用的吧,于是百度了下,大概是可能由于某些原因,导致部分代码没有写.但又怕忘了, 用//todo就可以做提示 ...

  8. uWSGI+django+nginx 的工作原理流程与部署历程

    一.前言 献给和我一样懵懂中不断汲取知识,进步的人们. 霓虹闪烁,但人们真正需要的,只是一个可以照亮前路的烛光 二.必要的前提 2.1 准备知识 django 一个基于python的开源web框架,请 ...

  9. ArcSDE 10 for SQL Server安装教程(含下载链接)

    亲测:ArcSDE 10.1适用于ArcGIS10.2的版本. 该版本支持SQL Server.Oracle.PostgreSQL等数据库连接 下载链接(含安装包和授权文件): 链接:https:// ...

  10. how2heap 源码及输出

    备个份,慢慢写总结 1 first_fit #include <stdio.h> #include <stdlib.h> #include <string.h> i ...