web前端请使用Vue路由相关知识动手实现Tab栏切换案例,要求如下: 4.1、创建一个components/Message.vue组件,用来展示页面内容。 4.2、创建三个子路由,分别是“待付款”“待发货”“待收货”页面,在每个子路由页面单独写出相应的内容。
时间: 2023-07-14 19:12:32 浏览: 277
好的,下面是实现步骤:
1. 在 Vue 项目中安装 vue-router:`npm install vue-router`
2. 在入口文件(main.js)中导入 vue-router,并使用 Vue.use() 安装插件:
```javascript
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')
```
3. 在 router.js 文件中定义路由:
```javascript
import Vue from 'vue'
import Router from 'vue-router'
import Message from '@/components/Message.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
redirect: '/payment'
},
{
path: '/payment',
component: Message,
children: [
{
path: '',
component: () => import('@/components/Payment.vue')
}
]
},
{
path: '/delivery',
component: Message,
children: [
{
path: '',
component: () => import('@/components/Delivery.vue')
}
]
},
{
path: '/receipt',
component: Message,
children: [
{
path: '',
component: () => import('@/components/Receipt.vue')
}
]
}
]
})
```
4. 创建 Message.vue 组件,用来展示页面内容:
```html
<template>
<div>
<div class="tabs">
<router-link to="/payment">待付款</router-link>
<router-link to="/delivery">待发货</router-link>
<router-link to="/receipt">待收货</router-link>
</div>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'Message'
}
</script>
<style>
.tabs {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
</style>
```
5. 创建三个子路由页面,分别是 Payment.vue、Delivery.vue 和 Receipt.vue,单独写出相应的内容。
例如,Payment.vue 可以这样写:
```html
<template>
<div>
<h2>待付款页面</h2>
<p>这里是待付款页面的内容。</p>
</div>
</template>
```
其他两个页面也可以类似地写出相应内容。
6. 在 App.vue 中使用 router-view 标签来展示路由的内容:
```html
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
```
这样,当访问 /payment 路径时,就会显示 Message.vue 组件中的待付款标签页和 Payment.vue 组件中的内容。其他两个标签页同理。
阅读全文
相关推荐

















