首頁 > web前端 > Vue.js > vue3如何使用keep alive實現前進更新後退銷毀

vue3如何使用keep alive實現前進更新後退銷毀

PHPz
發布: 2023-05-23 20:19:19
轉載
2076 人瀏覽過

keep alive實現前進更新後退銷毀

想要實現前進更新後退銷毀,核心在操作keep-alive的include。
具體做法就是當進入新頁面時將頁面name儲存,再次進入就將它之後的name刪除。

具體實現思路:

正常情況下頁面是線性前進的:

A->B->C->D

include數組資料[A,B,C,D]

當再次進入C,就認為是D返回C

include數組資料[A,B,C]

D頁面就被銷毀了,從而實現了後退銷毀

使用vuex保存include數組

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

const keep = {

    namespaced: true,

    state: () => {

        return {

            include: [],

        }

    },

    getters: {

        include(state) {

            return state.include

        },

    },

    mutations: {

        add(state, name) {

            let b = false

            let i = 0

            for (; i < state.include.length; ++i) {

                let e = state.include[i]

                if (e == name) {

                    b = true

                    break

                }

            }

            if (!b) {

                state.include.push(name)

            } else {

                state.include.splice(i + 1)

            }

        }

    },

    actions: {

    }

}

export default keep

登入後複製
在beforeEach中添加name

1

2

3

4

5

6

import store from "../store"

router.beforeEach((to, from,next) => {

// 页面name要和route 的name一样

  store.commit("keep/add", to.name)

  next()

})

登入後複製
include使用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<template>

  <router-view v-slot="{ Component }">

    <keep-alive :include="includeList">

      <component :is="Component" />

    </keep-alive>

  </router-view>

</template>

<script>

export default {

  computed: {

    includeList() {

      return this.$store.getters["keep/include"].join(",");

    },

  },

};

</script>

登入後複製

當然還有頁面循環跳躍的情況,通常是詳情頁

A->A->A->A 或A->B->C->A-> B->C

這種情況如果不需要儲存頁面,就用wacth監控$route變化重新請求介面

如果需要儲存頁面,就用動態路由addRoute新增新的路由

A1->A2->A3->A4

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

import time from "../views/time"

function copyObj(obj) {

    if (typeof obj == "object") {

        if (Array.isArray(obj)) {

            let arr = [];

            for (let item of obj) {

                arr.push(Object.assign(copyObj(item)));

            }

            return arr;

        } else if (obj == null) {

            return null;

        } else {

            let obj1 = {};

            for (let index in obj) {

                obj1[index] = copyObj((obj[index]));

            }

            return obj1;

        }

    } else if (typeof obj == "function") {

        return Object.assign(obj);

    } else if (typeof obj == undefined) {

        return undefined;

    } else {

        return obj;

    }

}

window.pushTime = function () {

    let t = new Date().getTime();

    let path = `/time/${t}`;

    // 深复制component

    time = copyObj(time)

    // component name要和route 的name一样

    time.name = path

    this.$router.addRoute({

        path,

        name: path,

        component: time,

    });

    this.$router.push({

        path,

    });

}

登入後複製

vue2用vue-navigation 非常好用

以上是vue3如何使用keep alive實現前進更新後退銷毀的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板