页面切换到默认显示顶部

方法一

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

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. HTML CSS整理笔记

    ——修改placeholder提示的样式: 1.除IE外通用写法 类名或标签名::placeholder {color: red;}2.加兼容前缀写法 css超出一行显示省略号:给定宽度(width: ...

  2. win32API多线程编程

    win32线程API 在Windows平台下可以通过Windows的线程库来实现多线程编程. 对于多线程程序可以使用Visual Studio调试工具进行调试,也可以使用多核芯片厂家的线程分析调试工具 ...

  3. python实现输入任意一个大写字母生成金字塔的示例

    输入任意一个大写字母,生成金字塔图形 def GoldTa(input): L = [chr(i) for i in range(65, 91)] # 大写字母A--Z idA = 65 # 从A开始 ...

  4. ArangoDB数据导入

    目录 arangoimp方法 参数解析 实例展示 python方法 单条导入 批量导入 1.arangoimp方法 参数解析 全局配置部分(Global configuration) --backsl ...

  5. Ubuntu16.04下升级Python到3.6

    转: 这里 有一篇帖子是说从源代码开始安装,这种方式原来尝试过,需要删除系统默认的软链命令,感觉比较粗暴,现在在想有没有更好的方式呢? 找到一个帖子:http://ubuntuhandbook.org ...

  6. MySQL字符集与排序规则总结

      字符集与排序规则概念 在数据库当中都有字符集和排序规则的概念, 很多开发人员甚至包括有些DBA都会将这个混淆,当然这个情况也有一些情有可原的原因.一来两者本来就是相辅相成,相互依赖关联: 另外一方 ...

  7. Spring Cloud gateway 网关四 动态路由

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  8. Covenant cc 用法

    新出现的cc框架,之前看hack the box有人用过,不过还是用cs比较多, 这里把之前的笔记搬运过来 ---   设置covenant git clone --recurse-submodule ...

  9. [考试反思]1103csp-s模拟测试99: 美梦

    可能这次考得好的原因就是熬夜颓废到不算太晚?(啪) 但是是真心困. 考前跟akt说:我希望今天考一点那种不用动脑子,就是一直码的题. 然后开门T1一道线段树维护单调栈的板子我就...了 当时调了一上午 ...

  10. 使用Typescript重构axios(三)——实现基础功能:处理get请求url参数

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...