• Vue返回组件(返回不同页面及保留滚动位置)


    this.$router.go(-1) ( VUE3写法 :proxy.$router.go(-1) )返回上一页最简单实用,现通过路由传参及keepAlive,实现返回保留滚动条位置

    router.js

    注:需要在列表页设置keepAlive: true

    1. import { createRouter, createWebHashHistory } from 'vue-router'
    2. const routes = [
    3. {
    4. path: '/',
    5. name: 'index',
    6. component: () => import('@/views/index.vue'),
    7. meta: {
    8. keepAlive: true // 需要缓存
    9. }
    10. }, {
    11. path: '/rank',
    12. name: 'rank',
    13. component: () => import('@/views/rank.vue'),
    14. }, {
    15. path: '/about/:orderid',
    16. name: 'about',
    17. component: () => import('@/views/about.vue')
    18. }
    19. ]
    20. const router = createRouter({
    21. history: createWebHashHistory(),
    22. routes
    23. })
    24. export default router

    App.vue

    1. <template>
    2. <div id="app">
    3. <router-view #default="{ Component, route }">
    4. <keep-alive>
    5. <component :is="Component" :key="route.name" v-if="route.meta.keepAlive"/>
    6. keep-alive>
    7. <component :is="Component" :key="route.name" v-if="!route.meta.keepAlive"/>
    8. router-view>
    9. div>
    10. template>
    11. <script setup>
    12. import {defineComponent,getCurrentInstance,ref,reactive,computed,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted,onErrorCaptured,onRenderTracked,onRenderTriggered,onActivated,onDeactivated,onServerPrefetch} from 'vue';
    13. //生命周期
    14. onBeforeMount(()=>{
    15. console.log('注册一个钩子,在组件被挂载之前被调用。onBeforeMount')
    16. })
    17. onMounted(()=>{
    18. console.log('注册一个回调函数,在组件挂载完成后执行。onMounted')
    19. })
    20. onBeforeUpdate(()=>{
    21. console.log('注册一个钩子,在组件即将因为响应式状态变更而更新其 DOM 树之前调用。onBeforeUpdate')
    22. })
    23. onUpdated(()=>{
    24. console.log('注册一个回调函数,在组件因为响应式状态变更而更新其 DOM 树之后调用。onUpdated')
    25. })
    26. onBeforeUnmount(()=>{
    27. console.log('注册一个钩子,在组件实例被卸载之前调用。onBeforeUnmount')
    28. })
    29. onUnmounted(()=>{
    30. console.log('注册一个回调函数,在组件实例被卸载之后调用。onUnmounted')
    31. })
    32. onErrorCaptured(() => {
    33. console.log('注册一个钩子,在捕获了后代组件传递的错误时调用。onErrorCaptured')
    34. })
    35. onRenderTracked(() => {
    36. console.log('注册一个调试钩子,当组件渲染过程中追踪到响应式依赖时调用。onRenderTracked')
    37. })
    38. onRenderTriggered(() => {
    39. console.log('注册一个调试钩子,当响应式依赖的变更触发了组件渲染时调用。onRenderTriggered')
    40. })
    41. onActivated(() => {
    42. console.log('注册一个回调函数,若组件实例是 缓存树的一部分,当组件被插入到 DOM 中时调用。onActivated')
    43. })
    44. onDeactivated(() => {
    45. console.log('注册一个回调函数,若组件实例是 缓存树的一部分,当组件从 DOM 中被移除时调用。onDeactivated')
    46. })
    47. onServerPrefetch(() => {
    48. console.log('注册一个异步函数,在组件实例在服务器上被渲染之前调用。onServerPrefetch')
    49. })
    50. script>
    51. <style scoped>
    52. style>

    需保存滚动条位置页.vue

    注意这两个onActivatedonDeactivated 这两个生命周期钩子函数

    1. <template>
    2. <div id="millia">
    3. <ul>
    4. <li @click="goTop">TOPli>
    5. <li @click="goRank">排行榜li>
    6. ul>
    7. div>
    8. template>
    9. <script setup>
    10. import {defineComponent,getCurrentInstance,ref,reactive,computed,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted,onErrorCaptured,onRenderTracked,onRenderTriggered,onActivated,onDeactivated,onServerPrefetch} from 'vue';
    11. //使用vue的getCurrentInstance 方法获取当前组件实例
    12. const { proxy } = getCurrentInstance()
    13. //当前滚动条位置
    14. let scroll = ref()
    15. const handleScroll = () => {
    16. scroll.value = document.documentElement && document.documentElement.scrollTop
    17. //console.log(scroll.value)
    18. }
    19. //窗口绑定监听方法
    20. window.addEventListener('scroll',handleScroll)
    21. //返回顶部
    22. const goTop = () => {
    23. document.getElementById('app').scrollIntoView({
    24. behavior: "smooth",
    25. block: "start",
    26. inline: "nearest"
    27. });
    28. }
    29. //去其他页同时传递首页滚动条位置
    30. const goRank = () => {
    31. proxy.$router.push({
    32. name:"rank",
    33. params:{
    34. goScroll: "goScroll",
    35. isScroll: scroll.value
    36. }
    37. })
    38. }
    39. //判断路由参数goScroll决定滚动条位置
    40. const isGoScroll = () => {
    41. if (proxy.$route.params.goScroll) {
    42. // console.log(proxy.$route.params.scroll)
    43. window.scrollTo(0, proxy.$route.params.scroll);
    44. }
    45. }
    46. //若 ,当组件被插入到 DOM 中时调用。onActivated'
    47. onActivated(() => {
    48. window.addEventListener('scroll',handleScroll);
    49. isGoScroll();
    50. if(scroll.value > 0){
    51. window.scrollTo(0, scroll.value);
    52. }
    53. }),
    54. //若 ,当组件从 DOM 中被移除时调用。onDeactivated'
    55. onDeactivated(() => {
    56. window.removeEventListener('scroll', handleScroll);
    57. })
    58. script>
    59. <style scoped>
    60. style>

    返回组件使用页.vue

    1. <template>
    2. <div id="millia">
    3. <go-top>go-top>
    4. <go-back :goBackData="goBackData">go-back>
    5. div>
    6. template>
    7. <script setup>
    8. import { defineComponent, getCurrentInstance, ref, reactive, computed, onMounted } from 'vue';
    9. import GoBack from '@/components/GoBack.vue'; //回顶部组件
    10. import GoTop from '@/components/GoTop.vue'; //返回组件
    11. //使用vue的getCurrentInstance 方法获取当前组件实例
    12. const { proxy } = getCurrentInstance()
    13. //goBack组件数据
    14. let goBackData = reactive({
    15. goBackParam:"",
    16. isScroll:"",
    17. })
    18. //获取路由参数
    19. const getRouter = () => {
    20. if(proxy.$route.params.goScroll){
    21. goBackData.goBackParam = proxy.$route.params.goScroll
    22. goBackData.isScroll = proxy.$route.params.isScroll
    23. }
    24. }
    25. onMounted(() => {
    26. getRouter();
    27. })
    28. script>
    29. <style scoped>
    30. style>

    GoBack返回组件

    1. <template>
    2. <div class="goBack" @click="goBack">
    3. 返回
    4. div>
    5. template>
    6. <script setup>
    7. import {getCurrentInstance,defineProps,defineEmits,defineExpose,ref,reactive} from 'vue';
    8. defineProps({
    9. goBackData: Object,
    10. //goBackParam及isScroll
    11. //goBackParam(参数类别)
    12. //为空值或者undefined后退一页
    13. //goScroll 需返回滚动条位置的页面,在name对应页面文件名
    14. //goIndex 返回首页
    15. //isScroll 滚动量
    16. })
    17. //使用vue的getCurrentInstance 方法获取当前组件实例
    18. const { proxy } = getCurrentInstance();
    19. console.log(proxy.goBackData.goBackParam)
    20. //根据参数判断返回
    21. const goBack = () => {
    22. console.log(proxy.goBackData.goBackParam)
    23. if(proxy.goBackData.goBackParam == '' || proxy.goBackData == undefined){
    24. proxy.$router.go(-1)
    25. }
    26. if(proxy.goBackData.goBackParam == "goScroll"){
    27. proxy.$router.push({
    28. name:"需保存滚动条页的文件名",
    29. params:{
    30. goScroll:"goScroll",
    31. scroll:proxy.goBackData.isScroll
    32. }
    33. })
    34. }
    35. if(proxy.goBackData.goBackParam == "goIndex"){
    36. proxy.$router.push({
    37. name:"index",
    38. //params:{
    39. // goScroll:"goIndex",
    40. //}
    41. })
    42. }
    43. }
    44. script>
    45. <style scoped>
    46. .goBack{position:fixed;right:0;bottom:0;display:flex;justify-content:center;align-items:center;z-index:8;background:#3398fb;color:#fff;font-size:1.4rem;padding:1rem 0;border-radius:0.5rem 0 0 0.5rem;writing-mode:vertical-rl;width:3rem;}
    47. .goBack .icon{height:3rem;width:3rem;}
    48. style>

    GoTop回顶部组件

    1. <template>
    2. <div class="goTop" @click="goTop">
    3. TOP
    4. div>
    5. template>
    6. <script setup>
    7. const goTop = () => {
    8. document.getElementById('app').scrollIntoView({
    9. behavior: "smooth",
    10. block: "start",
    11. inline: "nearest"
    12. });
    13. }
    14. script>
    15. <style scoped>
    16. .goTop{position:fixed;right:0;bottom:5.5rem;display:flex;justify-content:center;align-items:center;z-index:8;background:#ff58d9;color:#fff;font-size:1.4rem;padding:1rem 0;border-radius:0.5rem 0 0 0.5rem;writing-mode:vertical-rl;width:3rem;}
    17. .goTop .icon{width:3rem;}
    18. style>

  • 相关阅读:
    LeetCode 1684. 统计一致字符串的数目
    2022年Web前端开发流程和学习路线(详尽版)
    excel笔记
    直接安装WSL2及安装Ubuntu到F盘
    vue3使用cesium实现跟随弹框
    【前端面试必知】什么是虚拟DOM
    搞定蓝牙——第五篇(SMP)
    车载ECU嵌入式设备的诊断测试 – DTC
    LVS+keepalived
    【无标题】
  • 原文地址:https://blog.csdn.net/millia/article/details/126885684