Vue路由实现页面跳转

本文介绍如何使用Vue.js进行页面路由管理,包括默认加载'猜你喜欢'页面,实现商品详情页、分类页和购物车页面之间的跳转,以及返回功能。用户可以从猜你喜欢和购物车页面进入商品详情,点击分类进入分类页面,并在购物车和个人页面之间切换。点击返回按钮则能回到上一页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求:

  • 默认加载猜你喜欢页面
  • 猜你喜欢和购物车在一个页面,我的在另一个页面
  • 点击商品可以进入商品详情页
  • 点击返回可以返回之前的页面
<body>
    <div id="app">
        <router-view></router-view>
    </div>
    <!-- 默认渲染此模板 -->
    <template id="first">
        <main>
            <router-view></router-view>
            <ul>
                <li><router-link to="like">猜你喜欢</router-link></li>
                <li><router-link to="buy">购物车</router-link></li>
                <li><router-link to="my">我的</router-link></li>
            </ul>
        </main>
    </template>
    <template id="like">
        <main>
            <h1>这是猜你喜欢</h1>
            <ul>
                <li><router-link to="shiling">时令生鲜</router-link></li>
                <li v-for="(item,index) in list">
                	<router-link :to="item.path+'/'+item.id">{{item.con}}</router-link>
                </li>
            </ul>
        </main>
    </template>
    <template id="buy"><h1>这是购物车</h1></template>
    <template id="my">
        <div>
            <h1>这是我的页面</h1>
            <button @click="back()">返回</button>
        </div>
    </template>
    <template id="shiling">
        <div>
            <h1>这是时令生鲜</h1>
            <button @click="back()">返回</button>
        </div>
    </template>
    <template id="details"><h1>这是详情页</h1></template>

    <script src="js/vue.js"></script>
    <script src="js/vue-router.js"></script>
    <script>
        let first={template:"#first"};
        let like={
            template:'#like',
            //商品
            data(){
                return {
                    list:[
                        {
                            con:'擦玻璃神器',
                            path:'/details',
                            id:0
                        },
                        {
                            con:'红酒',
                            path:'/details',
                            id:1
                        }
                    ]
                }
            }
        };
        let buy={template:"#buy"};
        let my={
            template:'#my',
            methods:{
            	//返回上一层
                back(){
                    this.$router.go(-1);
                }
            }
        };
        let shiling={
            template:'#shiling',
            methods:{
            	//返回上一层
                back(){
                    this.$router.go(-1);
                }
            }
        }
        let details={
            template:'#details',
            mounted(){
                console.log(this.$route.params)
            }
        };
        const router=new VueRouter({
            routes:[
                {
                	//默认加载first模板
                    path:'/',
                    component:first,
                    //配置子路由
                    children:[
                        {
                            path:'/like',
                            component:like
                        },
                        {
                            path:'/buy',
                            component:buy
                        },
                        {
                            path:'/shiling',
                            component:shiling
                        }
                        
                    ],
                    redirect:'/like'
                },
                //商品详情页
                {
                    path:'/details/:vue',
                    component:details
                },
                {
                    path:'/my',
                    component:my
                },
                //以上路径都不符合重定向到like
                {
                    path:'/*',
                    redirect:'/like'
                }
            ]
        });
        new Vue({
            el:"#app",
            router:router
        })
    </script>
</body>

默认加载like
在这里插入图片描述
点击分类进入分类页
在这里插入图片描述

点击商品可以进入详情页
在这里插入图片描述
购物车
在这里插入图片描述
我的
在这里插入图片描述
点击返回:返回到上一层购物车页面
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值