App.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <div class="scroll-wrapper" ref="scroll" @scroll="scrollEvent">
  3. <app-bar ref="bar"></app-bar>
  4. <router-view v-slot="{ Component }">
  5. <keep-alive :include="keeps">
  6. <component :is="Component" class="scroll-content" ref="content" />
  7. </keep-alive>
  8. </router-view>
  9. </div>
  10. </template>
  11. <script>
  12. import AppBar from './components/AppBar.vue';
  13. import { computed } from 'vue';
  14. export default {
  15. // components: { AppBar },
  16. name: 'App',
  17. provide() {
  18. return {
  19. setKeeps: this.setKeeps,
  20. bar: computed(() => this.$refs.bar),
  21. content: computed(() => this.$refs.content),
  22. scrollWrapper: computed(() => this.$el),
  23. changeScroll: this.changeScroll,
  24. keeps: computed(() => this.keeps),
  25. bodyScroll: computed(() => this.bodyScroll)
  26. };
  27. },
  28. data() {
  29. return {
  30. checkEvent: null,
  31. keeps: [],
  32. bodyScroll: 0
  33. };
  34. },
  35. watch: {
  36. $route() {
  37. this.$nextTick(() => {
  38. this.$el.scrollTop = 0;
  39. });
  40. }
  41. },
  42. methods: {
  43. setKeeps(keep = [], isAdd = true) {
  44. let keeps = [...this.keeps];
  45. if (isAdd) {
  46. keeps = [...keeps, ...keep];
  47. } else {
  48. keeps = keeps.filter(item => {
  49. return !keep.includes(item);
  50. });
  51. }
  52. this.keeps = keeps;
  53. },
  54. changeScroll(scrollTop, isAnimate = false) {
  55. console.log(scrollTop);
  56. if (isAnimate) {
  57. this.$el.scrollTo({
  58. top: scrollTop,
  59. behavior: 'smooth'
  60. });
  61. } else {
  62. this.$el.scrollTop = scrollTop;
  63. }
  64. },
  65. scrollEvent() {
  66. // console.log(this.$el.scrollTop);
  67. this.bodyScroll = this.$el.scrollTop;
  68. }
  69. }
  70. };
  71. </script>
  72. <style lang="less" scoped>
  73. .scroll-content {
  74. min-height: 100vh;
  75. box-sizing: border-box;
  76. // padding-top: 46px;
  77. }
  78. .scroll-wrapper {
  79. background-color: @bg;
  80. }
  81. </style>