Vue【Keep-alive结合路由】

详细的将代码复制到html文件中 记得更改js文件地址

ul{
            list-style: none;
            margin: 0;
            padding: 0;
        }
        li{
            line-height: 30px;
        }
<div id="app">
        <!-- 
            keep-alive
            可以缓存组件包含动态组件和路由组件
            缓存的组件再次进入时不会触发生命周期也就是说组件不会重新渲染
            而且keep-alive会记录保留下来的页面的所有信息包括滚动高度
         -->
        <keep-alive>
            <router-view :key="$route.fullPath"></router-view>
        </keep-alive>
    </div>
    <script src="../../js/vue.js"></script>
    <script src="../../js/vue-router.js"></script>
    <script src="../../js/axios.min.js"></script>
    <script>
        const firstPage= {
            template: `
                <div id="firstPage">
                    <ul>
                        <router-link :to="{
                            path: '/secondPage',
                            query: {
                                id: item.id
                            }
                            
                        }" tag="li" v-for="item in topics" :key="item.id">{{item.title}}</router-link>
                    </ul>
                </div>
            `,
            data() {
                return {
                    topics: []
                }
            },
            created() {
                this.getTopics()
            },
            methods: {
                getTopics() {
                    axios.get('https://cnodejs.org/api/v1/topics').then(res => {
                        this.topics = res.data.data
                    })
                }
            }
        }
        const secondPage= {
            template: `
                <div id="secondPage">
                    <router-link to="/">返回</router-link>
                    <div v-html="detail.content"></div>
                </div>
            `,
            data() {
                return {
                    detail: {}
                }
            },
            created() {
                this.getTopicDetail()
            },
            methods: {
                getTopicDetail() {
                    axios.get(`https://cnodejs.org/api/v1/topic/${this.$route.query.id}`).then(res => {
                        this.detail = res.data.data
                    })
                }
            }
        }
        const routes = [
            {
                path: '/',
                component: firstPage
            },
            {
                path: '/secondPage',
                component: secondPage
            }
        ]
        let router = new VueRouter({
            routes,
            scrollBehavior (to, from, savedPosition) {
                // console.log(to)
                // console.log(from)
                // console.log(savedPosition) // (通过浏览器的 前进/后退 按钮触发)
                return {x: 0, y:0}
            }
        })
        var vm = new Vue({
           el:'#app',
           router,
           data:{},
           methods:{}
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值