Giter Site home page Giter Site logo

hezhongfeng / vue-page-stack Goto Github PK

View Code? Open in Web Editor NEW
670.0 13.0 77.0 21.65 MB

Routing and navigation for your Vue SPA. Vue3.0 单页应用导航管理器

Home Page: https://vue-page-stack-example.onrender.com

JavaScript 98.62% Shell 1.38%
vue vue-router cache stack page vue-navigation vue-spa translation vue3

vue-page-stack's Introduction

vue-page-stack

v3.1.0

  1. 移除了 url 上的参数 stack-key
  2. 因为 Vue3.x 对内置组件有特殊处理,所以目前不能和 Transition 一起使用

This is the version of Vue3.0, Vue2.0 please click this link

npm version

English | 简体中文


A Vue3 SPA navigation manager,cache the UI in the SPA like a native application, rather than destroy it.

Example

preview

demo code

Features

  • 🐉 Extend on vue-router, original navigation logic remains the same
  • push or forward renders the page and the newly rendered page is stored in Stack
  • 🏆 back or go(negative) when the previous pages are not re-rendered, but read from the Stack, and these pages retain the previous content state, such as form content, scrollbar scroll position, etc.
  • 🏈 back or go(negative) removes the unused pages from the Stack
  • 🎓replace will update the current page in the Stack
  • 🎉 The activated hook function is triggered when going back to the previous page
  • 🚀 Support for browser backward and forward events
  • 🐰 Provides routing direction changes and can add different animations when going forward and backward

The difference between VuePageStack and KeepAlive

  • 🌱 VuePageStack does not provide include, exclude and max parameters, because VuePageStack wants to achieve a complete page stack management, only in order in and out
  • 🪁 KeepAlive will keep caching the page after it has been cached, and VuePageStack will help destroy the extra pages based on the page stack hierarchy
  • 🧬 KeepAlive enters (not returns) the same route page and continues to reuse the previously cached page, while VuePageStack re-renders the page

Installation and use

Installation

pnpm install vue-page-stack

use

import { createApp } from 'vue';
import { VuePageStackPlugin } from 'vue-page-stack';

const app = createApp(App);

// router is necessary
app.use(VuePageStackPlugin, { router });
// App.vue
<template>
  <router-view v-slot="{ Component }">
    <vue-page-stack @back="onBack" @forward="onForward">
      <component :is="Component" :key="$route.fullPath"></component>
    </vue-page-stack>
  </router-view>
</template>

<script setup>
const onBack = () => {
  console.log('back');
};

const onForward = () => {
  console.log('forward');
};
</script>

API

install

use Vue.use to install vue-page-stack

import { VuePageStackPlugin } from 'vue-page-stack';

//...
app.use(VuePageStackPlugin, { router });

Options description:

Attribute Description Type Accepted Values Default
router vue-router instance Object vue-router instance -
name VuePageStack name String 'VuePageStack' 'VuePageStack'

forward or back

If you want to make some animate entering or leaving, vue-page-stack offers stack-key-dir to judge forward or backward.

// App.vue
<vue-page-stack @back="onBack" @forward="onForward">
  <component :is="Component" :key="$route.fullPath"></component>
</vue-page-stack>

example

Notes

Changelog

Details changes for each release are documented in the release notes.

Principle

Getting the current page instance refers to the keep-alive section of Vue.

Thanks

The plug-in draws on both vue-navigation and vue-nav,Thank you very much for their inspiration.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

hezf
hezf

🎨
李娜
李娜

📖
余小磊
余小磊

💻
yellowbeee
yellowbeee

💻

vue-page-stack's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar hezhongfeng avatar woshilina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-page-stack's Issues

首页tab切换问题

首页有多个tab,来回切换时,tab页每次都是新的
路由:

{
name: "foot",
path: "/foot",
meta: { title: "底部导航" },
component: () => import("@/views/main/foot.vue"),
children: [
{
name: "home",
path: "/home",
meta: { title: "首页" },
component: () => import("@/views/main/home.vue")
},
{
name: "projectInfo",
path: "/projectInfo",
meta: { title: "项目信息" },
component: () => import("@/views/main/projectInfo.vue")
},
{
name: "workPlan",
path: "/workPlan",
meta: { title: "工作计划" },
component: () => import("@/views/main/workPlan.vue")
},
{
name: "task",
path: "/task",
meta: { title: "员工任务" },
component: () => import("@/views/main/task.vue")
},
{
name: "personal",
path: "/personal",
meta: { title: "我的" },
component: () => import("@/views/main/personal.vue")
}
]
}

页面:

<router-view />
<van-tabbar v-model="active">
  <van-tabbar-item to="/projectInfo">
    <span>项目信息</span>
  </van-tabbar-item>
  <van-tabbar-item to="/workPlan">
    <span>工作计划</span>
  </van-tabbar-item>
  <van-tabbar-item to="/home">
    <span>首页</span>
  </van-tabbar-item>
  <van-tabbar-item to="/task">
    <span>员工任务</span>
  </van-tabbar-item>
  <van-tabbar-item to="/personal">
    <span>我的</span>
  </van-tabbar-item>
</van-tabbar>

如果router-view 用vue-page-stack 包起来就什么都不显示了,白屏

vue-page-stack:Clear all page cache

Hello, can you provide a method to clear all the router cache.
Because I want to router back to root page (the home page) but It existed the cached page stacks that I unwated.
So,this methods is usefully.

eg: this.$pageStack.cleanRoutes();

路由跳转catch显示undefined

报错信息

TypeError: Cannot read property 'catch' of undefined
    at _t.router.push (vue-page-stack.common.js:1619)

报错位置

return routerPush(location).catch(function (error) {
      if (error !== undefined) {
        console.log(error);
      }});

可以给某些页面选择不配置stack-key吗

遇到公众号ios分享的问题,从页面Alocation.href跳转了页面B,但是页面B带了stack-key,导致签名不成功。用户可以自主选择给页面B不配置stack-key吗?

hash

如果使用hash模式的路由,是无法相同路由继续 跳转的吗?

stack-key

stack-key希望后续可以从url中隐藏

页面跳转stack-key的值没变, 难道是因为/test/a和/test/b的路由没有层级关系?

我在官方示例的基础上做如下改动:

1、router\index.js添加新路由


{
    path: '/test',
    component: BlankLayout,
    redirect: '/test/a',
    children: [
      {
        path: 'a',
        component: () => import('@/views/test/a.vue')
      },
      {
        path: 'b',
        component: () => import('@/views/test/b.vue')
      }
    ]
  },

其中BlackLayout.vue是一个空布局页面,代码如下:

<template>
    <router-view />
</template>

<script>

  export default {
    name: "BlankLayout",
  }
</script>

<style scoped>

</style>

2、创建对应的2个页面

/views/test/a.vue 页面:

<template>
    <div>
      <button @click="toB">进入/test/b页面</button>
    </div>
</template>

<script>
    export default {
      name: 'APage',
      created(){
        console.log("/test/a created")
      },
      beforeDestroy(){
        console.log("/test/a beforeDestroy")
      },
      methods: {
          toB(){
            this.$router.push('/test/b')
          }
      }
    }
</script>

<style scoped>

</style>

/views/test/b.vue页面:

<template>
    <div>
      <button @click="toHelloA">进入/hello/a页面</button>
    </div>
</template>

<script>
    export default {
      name: 'BPage',
      created(){
        console.log("/test/b created")
      },
      beforeDestroy(){
        console.log("/test/b beforeDestroy")
      },
      methods: {
        toHelloA(){
            this.$router.push('/hello/a')
          }
      }
    }
</script>

<style scoped>

</style>

3、修改首页的index/index.vue页面

onExperience() {
      this.$router.push('/test/a');
    },

4、测试发现从/test/a页面进入/test/b页面时,路由参数stack-key未变,a.vue页面的beforeDestory()方法被调用了,导致页面被销毁了

猜测:难道是因为/test/a和/test/b的路由没有层级关系?

能否强制缓存某个页面?

使用场景: 移动端底部 tab 导航

底部 tab 导航不管是 push 或者是 replace 操作, 页面都应该被缓存下来

关于cube-scroll的问题

如果不使用cube-scroll这个组件,是不是就无法保持记录滚动,我看之前issues有提到这个问题,但是看得不太明白。

如何清空View栈

我现在有个需求:
登录后在使用过程中某个时间点,这个账号被顶了。
那么我需要回到登录页面。
这个时候我想把所有的页面清除,然后再把登录页push出来
请问怎么做呢?
另外,

let UIStack = this.$pageStack.getStack();

我拿不到pageStack

路由切换有BUG

没办法截图。来回切换home与关于,两页面内容会重叠。

纠正文档错误

  1. 二级标题_Vue 单页应用导航管理器_中英文不对应,建议
    中文:Vue 单页应用导航管理器,像原生app一样管理页面栈而不是销毁
    英文:A Vue SPA navigation manager,cache the UI in the SPA like a native application, rather than destroy it

  2. 示例源码 英文:demo code

  3. 功能特性 第二条 英文:When a page is re-rendered as a push or forward, a newly rendered page will be added to the Stack

CDN引入vue和vue-router引起错误

cdn引入的vue和vue-router,此时import 'vue-page-stack'会导致报错Cannot read property 'router' of undefined
是不是src/index.js中58-60行引起的?这里没有router实例

路由嵌套的问题

用了这个插件,发现在路由嵌套的时候是有问题的,包裹在第二层的 router-view ,这一层路由的视图跳转的时候没办法显示。

每次将tab切回来都会重新创建一个页面

我的主页面下面有四个tab,每次点击tab按钮都是通过this.$router.push(/xxx);进行页面跳转。
用了这个组件之后每次切换前一个页面都不会destroy,但是每次切回来这个页面又会重新create一遍,来回切换会生成很多无用的页面。
帮我看看是我用错了还是组件有问题?
63BC6076-FE05-4624-B422-269D51A9BD36
CF6D7692-DB8C-4A38-BF73-CA09AF203892
B0C28CA0-AEE1-4CB0-A172-8B6A488B7E46

使用插件,有办法做到选项卡页面跳转达到缓存的效果吗?

场景:A页面跳转B页面,B页面跳转C页面,这时候路由记录有三个。我再使用push跳转至B页面,这时候我携带路由记录B页面的key值,从缓存中获取打开的B页面,但是路由记录中的C却释放了,导致我跳转C页面是重新加载。有办法解决这一问题吗?还是说我写的方式有问题。

滚动位置缓存问题

请问滚动位置缓存是必须依赖 cube-scroll 吗?

https://github.com/hezhongfeng/vue-page-stack-example/blob/master/src/views/main/MainList.vue

把以上文件中的 cube-scroll 换成普通滚动,返回时就无法记住滚动位置了。

<template>
  <div class="main-list">
    <stack-header :title="$t('home')"></stack-header>
    <!-- <cube-scroll ref="scroll0" :data="list"> -->
    <div style="height: 500px; overflow-y: scroll">
      <main-item v-for="(item, index) in list" :key="index" :item="item" :index="index" @click="onClick(item)"></main-item>
    </div>
    <!-- </cube-scroll> -->
  </div>
</template>

自定义stack-key-dir

可以增加支持外部传入stack-key-dir来控制动画吗,
我现在index.js中修改的,
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.