vue项目页面切换到默认显示顶部
页面切换到默认显示顶部
方法一
使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样。
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项目页面切换到默认显示顶部的更多相关文章
- vue项目页面空白
vue项目页面空白 今天新建项目,然后发现路由也改了 app.vue里面也是啥都没有, 但是访问http://localhost:8080/#/login 能访问 里面确实空白的 错误: 错误原因: ...
- Vue项目页面跳转时候的,浏览器窗口上方的进度条显示
1.安装: cnpm install --save nprogress 2.在main.js中引入: import NProgress from 'nprogress' import 'nprogre ...
- 97、进入ScrollView根布局页面,直接跳到页面底部,不能显示顶部内容
API使用:http://www.cnblogs.com/over140/archive/2011/01/27/1945964.html 以ScrollView为根的部局,不能从顶部显示其包含的页面内 ...
- 基于Vue的页面切换左右滑动效果
HTML文本页面: <template> <div id="app> <transition :name="direction" mode= ...
- Vue 动态控制页面中按钮是否显示和样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue 项目路由跳转后显示不同的title
1.在router/index.js的每个路由中配置title 2.在项目中运行命令 npm install vue-wechat-title --save 安装插件(在 package.json文件 ...
- IIS部署vue项目页面刷新404,url重写问题解决办法
这里需要用到URL重写工具 --URL Rewrite(默认没有,需要自己下载安装) 如果IIS上默认有安装Web平台安装程序,我们可以使用平台自动安装URL Rewrite重写工具,打开IIS在管理 ...
- 【Vue中的坑】vue项目中动态绑定src不显示图片解决方法
v-for绑定src的数据如下: data() { return { img_src:"../../assets/images/mirror-service.png" } } 渲染 ...
- vue项目build后font-awesome不显示问题
解决办法: 修改build目录下的utils.js:添加 publicPath: '../../' // Extract CSS when that option is specified // (w ...
随机推荐
- python编程系列---Pycharm快捷键(更新中....)
以下是我常用到的Pycharm快捷键(还有很多,只是我暂时用的最多的就这些): 在开发过程中,经常使用一些快捷键会大大提高开发效率,不要因为看这多而不用,常用的就那些,用得多就都记住了,脱离鼠标,逼格 ...
- C#发送电子邮件(SMTP)及outlook.com账号之概要
这是关于c#发送电子邮件(SMTP)的技术笔记,以”简报“形式呈现. 因为最后成功通过outlook.com发送了邮件,所以,我觉得还是有必要 记录一下其中的要点. 一.技术核心 .net Frame ...
- Bugku SQL注入2的思考
网络安全初学者,欢迎评论交流学习,若内容中有错误欢迎各位指正. 题目地址:http://123.206.87.240:8007/web2/ 题目提示:都过滤了绝望吗?,提示 !,!=,=,+,-,^, ...
- 以np.concatenate为主题,谈谈numpy数组按维度合并的问题
1.引言 最近在做多模态融合的图像问题,其中最需要解决的就是不同模态的图像用什么方法进行融合,最简单也最直观的方法就是采用合并数组的方法,将不同模态的图像合并为多通道进行处理.在一些论文中,比如< ...
- linq一般用法
最一般的用法 var rows = from c in dataTrue.AsEnumerable() from t in dataPre.AsEnumerable() ].ToString().St ...
- Spring Boot Mail 实现邮件发送
此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件. 邮件服务在开发中非常常见,比如用邮件注册账号.邮件作为找回密码的途径.用于订阅内容定期邮件推送等等,下面就简 ...
- PHP 类中使用全局变量和全局常量
<?php $global_var = "var"; define('global_const', 'const'); class Test { public $_var; ...
- 解决node.js使用fs读取文件出错
今天配接口,使用fs模块读取json出现了错误'no such file or directory',然后经查终于解决,特此记录. 使用nodejs的fs模块读取文件时习惯用相对路径,但是运行的时 ...
- 明解C语言 入门篇 第三章答案
练习3-1 #include <stdio.h> int main() { int x; int y; puts("请输入两个整数."); printf("整 ...
- Python中使用字典的几个小技巧
1 解包 所谓解包,就是将字典通过 ** 操作符转为 Key=Value 的形式,这种形式可以直接传给函数作为关键字参数. 说说适用的几种情况. 1.1 搜索拼接条件 当应用中使用类似 SQLAlch ...