vue中路由返回上一个页面,恢复到上一个页面的滚动位置
第一步:路由文件的配置(对你所需要的vue文件进行保存缓存标志的添加)
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Message from '@/components/Message'
import Search from '@/components/Search'
import Home from '@/components/bottomBar/Home'
import Person from '@/components/bottomBar/Person'
import Release from '@/components/bottomBar/Release'
import Collection from '@/components/bottomBar/Collection' Vue.use(Router);
const routes =[
{
path: '/',
name: 'HelloWorld',
component: HelloWorld,
children: [
{
path:'home',
component:Home, meta: {
title: 'home',
keepAlive: true
}
},
{
path:'person',
component:Person
},
{
path:'release',
component:Release
},
{
path:'collection',
component:Collection
}
]
},
{
path:"/message",
component:Message
},
{
path:"/search",
component:Search
}
];
const router = new Router({
routes,
}); export default router
第二步:router-view的设置
<template>
<div id="index">
<keep-alive >
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
<van-tabbar v-model="active">
<van-tabbar-item icon="home" to="/home">首页</van-tabbar-item>
<van-tabbar-item icon="records" to="/release">发布</van-tabbar-item>
<van-tabbar-item icon="idcard" to="/collection">收藏</van-tabbar-item>
<van-tabbar-item icon="contact" to="/person" :dot="true">个人中心</van-tabbar-item>
</van-tabbar> </div>
</template> <script>
export default {
data () {
return {
value:"",
active:
}
},
mounted(){
this.$router.push("/home")
},
methods:{ }
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less">
#index{
.van-tabbar{
height: .1rem;
border-top: 1px solid #eee;
.van-tabbar-item--active{
color: orange;
}
.van-tabbar-item__icon{
font-size: .5rem;
.van-info{
padding: .05rem .1rem;
line-height: .3rem;
right:-.1rem;
}
}
}
}
</style>
第三步:在你需要保存位置的vue文件里面进行操作
<template>
<div class="htmlGlobal">
<div id="home">
<van-row>
<van-col span="" id="message" @click.native="goToMessageHtml">
<van-icon name="chat" :class="{'dot':true}"/>
</van-col>
<van-col span="" id="search">
<van-search
placeholder="搜你感兴趣的"
v-model="value"
background="orange"
@focus="goToSearchHtml"
/>
</van-col>
<van-col span=""></van-col>
</van-row>
</div>
<div id="noticeBar">
<van-notice-bar mode="closeable"
:text=noticeValue
left-icon="http://img.yzcdn.cn/vant/volume.png"
/>
</div>
<div id="vanSwipe">
<van-swipe :autoplay="" indicator-color="orange">
<van-swipe-item></van-swipe-item>
<van-swipe-item></van-swipe-item>
<van-swipe-item></van-swipe-item>
<van-swipe-item></van-swipe-item>
</van-swipe>
</div>
<div id="vanTabs" >
<van-tabs swipeable v-model="tabActive" :class="{'fixed':fixed}">
<van-tab v-for="(item,index) in tabsArr" :title="item.name" :key="index">
</van-tab>
</van-tabs>
<ShopList :index="tabActive"></ShopList>
</div>
</div>
</template> <script> import ShopList from '../ShopList'
export default {
beforeRouteLeave(to, from, next){
this.$store.commit("set_scrollTop",this.scroll);
next();
},
activated () {
this.$nextTick(function(){
let position = this.$store.state.scrollTop; //返回页面取出来
window.scrollTo(, position); })
},
components:{
ShopList
},
data(){
return{
scroll:,
fixed:false,
value:'',
//二手物品的分类信息
tabsArr:[
{
name:"书籍",
requestCode:"",
},
{
name:"生活百货",
requestCode:"",
},
{
name:"乐器",
requestCode:"",
},
{
name:"数码",
requestCode:"",
},
{
name:"服饰鞋包",
requestCode:"",
},
{
name:"美妆捡漏",
requestCode:"",
},
{
name:"运动户外",
requestCode:"",
},
{
name:"健身器材",
requestCode:,
}
],
noticeValue:"足协杯战线连续第2年上演广州德比战",
tabActive:
}
},
methods:{
goToMessageHtml(){
this.$router.push("/message");
},
goToSearchHtml(){
this.$router.push("/search");
},
handleScroll(){
if(document.getElementById('vanTabs')){
this.scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
var offsetTop = document.getElementById('vanTabs').offsetTop;
if (this.scroll > offsetTop) { this.fixed = true;
} else {
this.fixed = false
}
}
} }, destroyed () {
window.removeEventListener('scroll', this.handleScroll)
},
mounted(){
this.$nextTick(()=>{
window.addEventListener('scroll', this.handleScroll)
})
}
}
</script> <style lang="less">
.fixed{
position: fixed !important;
top: ;
left: ;
}
.htmlGlobal{
background-color: #ccc;
box-sizing: border-box;
}
#home{
background-color:orange;
height: 1rem;
box-sizing: border-box;
padding: .1rem;
.van-row{
height: %;
#message{
height: %;
.van-icon{
font-size: .5rem;
top: -.4rem;
left: .13rem;
color: #fff;
.van-info{
padding: .05rem .1rem;
line-height: .3rem;
right:-.05rem;
}
} }
#search{
.van-search{
padding: ;
padding-top: .1rem;
.van-cell{
border-radius: 20px;
height: .6rem;
.van-cell__left-icon{
font-size: .4rem;
position: absolute;
top: .15rem;
bottom: .1rem;
}
.van-field__body{
position: absolute;
left: .5rem;
top: .05rem;
bottom: .1rem;
right: ;
input{
font-size: .3rem;
}
}
}
}
}
} }
#noticeBar{
.van-notice-bar{
height: .8rem;
.van-notice-bar__left-icon {
height: %;
line-height: .8rem;
img{
width: .4rem;
height: .4rem;
vertical-align: middle;
}
}
.van-notice-bar__wrap{
height: %;
line-height: .8rem;
.van-notice-bar__content{
font-size: .3rem;
}
}
.van-icon.van-icon-close.van-notice-bar__right-icon{
font-size: .35rem;
right: .1rem;
top: .22rem;
}
}
}
#vanSwipe{
.van-swipe{
/*margin: 0 .3rem;*/
height:3rem;
.van-swipe-item{
background-color: deepskyblue;
border-radius: 5px;
}
}
}
#vanTabs{
.van-tabs{ }
}
</style>
vue中路由返回上一个页面,恢复到上一个页面的滚动位置的更多相关文章
- vue中路由按需加载的几种方式
使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hello' import ...
- vue中,对象数组多层嵌套时,更新数据更新页面
vue中的对象和数组的元素直接赋值修改时,是不能响应到view中去的 1.对象更新 this.a={title:'列表1’}; this.a.title='列表2’; <h1>{{a.ti ...
- Vue中路由的使用
在Vue中动态挂载组件 首先需要安装 cnpm install vue-router --save 在main.js中引入VueRouter 并使用 定义一个路由 创建router实例 通过rout ...
- Vue中ajax返回的结果赋值
这是第二次在项目中遇到此问题,ajax请求成功后在success函数中为Vue实例data里的变量赋值,却失败了 new Vue({ el:'#app', data:{ msg:'' }, creat ...
- VUE中实现iview的图标效果时遇到的一个问题
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available ...
- vue-router 页面切换后保持在页面顶部而不是保持原先的滚动位置的办法
vue-router有提供一个方法scrollBehavior,它可以使切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. 这个功能只在 HTML5 history 模 ...
- vue中路由传参的方式
一.params的类型: 配置路由格式: /router/:id 传递的方式: 在path后面跟上对应的值 传递后形成的路径: /router/123, /router/abc 通过:to字符串拼接的 ...
- 【每天学一点-06】在Vue中使用Vant-Picker选择器,并且给选择器添加一个类似Antd-Select-showSearch的搜索功能
一.在Vant文档中,Picker组件的API中是没有showSearch这一选项的 1.Vant-Picker 文档 2.Antd-Select 文档 3.需要完成的需求 4.因为在H5项目中出现了 ...
- Vue中data返回对象和返回值的区别
速记:粗浅的理解是,事件的结果是影响单个组件还是多个组件.因为大部分组件是要共享的,但他们的data是私有的,所以每个组件都要return一个新的data对象 返回对象的时候 <!DOCTYPE ...
随机推荐
- ARKit从入门到精通
ARKit从入门到精通(10)-ARKit让飞机绕着你飞起来 ARKit从入门到精通(9)-ARKit让飞机跟着镜头飞起来 ARKit从入门到精通(8)-ARKit捕捉平地 ARKit从入门到精通(7 ...
- sqlserver中一些常用的函数总结
去掉空格方面 LTRIM('内容'):去掉字符串左边的空格 RTRIM('内容'):去掉右边的空格 LTRIM(RTRIM('内容')):去掉字符串左边和右边的空格 REPLACE(‘内容’,' ', ...
- HttpRunner接口自动化测试框架
简介 2018年python开发者大会上,了解到HttpRuuner开源自动化测试框架,采用YAML/JSON格式管理用例,能录制和转换生成用例功能,充分做到用例与测试代码分离,相比excel维护测试 ...
- Ubuntu Touch On Nexus4 Manual Install (手动安装) under Gentoo
Table of Contents 1. 准备工作: 2. Saucy Salamander 3. 刷入 最新 版Touch 最近手里的 Nexus 4 手机一直闲置,它的配置要比我六年前买的笔记本还 ...
- 一个文件系统过滤驱动的demo
因为没写过FSD过滤驱动,所以拿来练练手,没有什么技术含量.参考自Win内核安全与驱动开发. 先梳理一下大概的流程,就是怎么去绑定设备栈.怎么去过滤各种请求的. 首先肯定是要绑定设备栈的,来看下怎么绑 ...
- Django实战(7):改造ProductList界面
有了上一节关于Django模板的基础,改造界面就很容易理解了.将界面设计师设计的页面中的内容根据复用程度分别放到基础模板base.html和专用模板productlist.html中. depot/t ...
- umount /dev/shm
[root@test ~]# umount /dev/shm umount: /dev/shm: device is busy. (In some cases useful info a ...
- Maven项目使用阿里云的Maven库
Maven项目下载一些jar包非常慢,有时候一个项目能下一个上午,因此可以考虑使用阿里云的Maven库,因为是国内的,所以下载速度非常酷 单个项目使用阿里云的Maven库: pom文件中 <!- ...
- Python并发编程系列之常用概念剖析:并行 串行 并发 同步 异步 阻塞 非阻塞 进程 线程 协程
1 引言 并发.并行.串行.同步.异步.阻塞.非阻塞.进程.线程.协程是并发编程中的常见概念,相似却也有却不尽相同,令人头痛,这一篇博文中我们来区分一下这些概念. 2 并发与并行 在解释并发与并行之前 ...
- 【原创】MySQL Replay线上流量压测工具
一. 背景 去年做过一次mysql trace 重放的测试,由于performance schema本身采集样本的长度等限制,实际回放的成功率比较低. 最近找到一款开源的工具,基于TCPCopy实现了 ...