请使用Vue路由相关知识动手实现Tab栏切换案例,要求如下: 4.1、创建一个components/Message.vue组件,用来展示页面内容。 4.2、创建三个子路由,分别是“待付款”“待发货”“待收货”页面,在每个子路由页面单独写出相应的内容。
时间: 2023-08-31 19:20:22 浏览: 356
### 回答1:
好的,首先需要在路由文件中定义三个子路由:
```
import Vue from 'vue'
import Router from 'vue-router'
import Message from '@/components/Message'
import Unpaid from '@/components/Unpaid'
import Undelivered from '@/components/Undelivered'
import Unreceived from '@/components/Unreceived'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Message',
component: Message,
children: [
{
path: 'unpaid',
name: 'Unpaid',
component: Unpaid
},
{
path: 'undelivered',
name: 'Undelivered',
component: Undelivered
},
{
path: 'unreceived',
name: 'Unreceived',
component: Unreceived
}
]
}
]
})
```
接下来,在Message.vue组件中添加Tab栏代码:
```
<template>
<div>
<div class="tabs">
<router-link to="/unpaid" class="tab" active-class="active">待付款</router-link>
<router-link to="/undelivered" class="tab" active-class="active">待发货</router-link>
<router-link to="/unreceived" class="tab" active-class="active">待收货</router-link>
</div>
<router-view></router-view>
</div>
</template>
<style scoped>
.tabs {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
.tab {
font-size: 16px;
padding: 10px;
cursor: pointer;
border-bottom: 2px solid #ccc;
}
.active {
border-bottom-color: #333;
}
</style>
```
最后,编写三个子路由对应的组件Unpaid.vue、Undelivered.vue、Unreceived.vue,分别展示相应的页面内容即可。
完整代码如下:
router/index.js
```
import Vue from 'vue'
import Router from 'vue-router'
import Message from '@/components/Message'
import Unpaid from '@/components/Unpaid'
import Undelivered from '@/components/Undelivered'
import Unreceived from '@/components/Unreceived'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Message',
component: Message,
children: [
{
path: 'unpaid',
name: 'Unpaid',
component: Unpaid
},
{
path: 'undelivered',
name: 'Undelivered',
component: Undelivered
},
{
path: 'unreceived',
name: 'Unreceived',
component: Unreceived
}
]
}
]
})
```
components/Message.vue
```
<template>
<div>
<div class="tabs">
<router-link to="/unpaid" class="tab" active-class="active">待付款</router-link>
<router-link to="/undelivered" class="tab" active-class="active">待发货</router-link>
<router-link to="/unreceived" class="tab" active-class="active">待收货</router-link>
</div>
<router-view></router-view>
</div>
</template>
<style scoped>
.tabs {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
.tab {
font-size: 16px;
padding: 10px;
cursor: pointer;
border-bottom: 2px solid #ccc;
}
.active {
border-bottom-color: #333;
}
</style>
```
components/Unpaid.vue
```
<template>
<div>
<h2>待付款页面</h2>
<p>这里是待付款页面的内容。</p>
</div>
</template>
```
components/Undelivered.vue
```
<template>
<div>
<h2>待发货页面</h2>
<p>这里是待发货页面的内容。</p>
</div>
</template>
```
components/Unreceived.vue
```
<template>
<div>
<h2>待收货页面</h2>
<p>这里是待收货页面的内容。</p>
</div>
</template>
```
### 回答2:
在Vue中实现Tab栏切换,需要使用Vue Router插件来管理路由。
首先,安装Vue Router插件:
```
npm install vue-router
```
然后,在main.js文件中引入Vue Router并配置路由:
```javascript
import Vue from 'vue'
import VueRouter from 'vue-router'
import Message from './components/Message.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/待付款', component: Todo },
{ path: '/待发货', component: Doing },
{ path: '/待收货', component: Done }
]
const router = new VueRouter({
routes
})
new Vue({
router,
render: h => h(App)
}).$mount('#app')
```
接下来,在App.vue组件中添加Tab栏:
```html
<template>
<div id="app">
<ul>
<li><router-link to="/待付款">待付款</router-link></li>
<li><router-link to="/待发货">待发货</router-link></li>
<li><router-link to="/待收货">待收货</router-link></li>
</ul>
<router-view></router-view>
</div>
</template>
```
最后,创建Message.vue组件并分别写出三个子路由的内容,分别是“待付款”、“待发货”和“待收货”:
```html
<template>
<div>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: '',
content: ''
}
},
created() {
if (this.$route.path === '/待付款') {
this.title = '待付款'
this.content = '待付款页面内容'
} else if (this.$route.path === '/待发货') {
this.title = '待发货'
this.content = '待发货页面内容'
} else if (this.$route.path === '/待收货') {
this.title = '待收货'
this.content = '待收货页面内容'
}
}
}
</script>
```
这样就完成了Tab栏切换的实现,每个子路由页面都有自己相应的内容。当点击不同的Tab时,会根据路由切换到对应的子页面,并展示相应的内容。
### 回答3:
首先,需要在路由文件中配置三个子路由,同时引入相应的组件。
```javascript
//导入组件
import Message from '@/components/Message.vue'
import Payment from '@/components/Payment.vue'
import Delivery from '@/components/Delivery.vue'
import Reception from '@/components/Reception.vue'
//配置路由规则
const routes = [
{
path: '/',
redirect: '/payment' //默认进入待付款页面
},
{
path: '/',
component: Message,
children: [
{
path: 'payment',
component: Payment,
},
{
path: 'delivery',
component: Delivery,
},
{
path: 'reception',
component: Reception,
}
]
}
]
//创建路由实例
const router = new VueRouter({
mode: 'history',
routes
})
```
接下来,在Message.vue组件中添加一个Tab栏,用于切换不同的子路由页面。
```html
<template>
<div>
<div class="tabs">
<router-link to="/payment">待付款</router-link>
<router-link to="/delivery">待发货</router-link>
<router-link to="/reception">待收货</router-link>
</div>
<router-view></router-view>
</div>
</template>
<style>
.tabs {
display: flex;
}
.tabs a {
margin-right: 10px;
padding: 5px;
border: 1px solid #ccc;
text-decoration: none;
color: #333;
}
.tabs a.router-link-exact-active {
color: #f00;
}
</style>
```
最后,在各个子路由对应的组件中分别编写相应的内容。
Payment.vue:
```html
<template>
<div>
<h2>待付款页面</h2>
<p>这是待付款页面的内容</p>
</div>
</template>
```
Delivery.vue:
```html
<template>
<div>
<h2>待发货页面</h2>
<p>这是待发货页面的内容</p>
</div>
</template>
```
Reception.vue:
```html
<template>
<div>
<h2>待收货页面</h2>
<p>这是待收货页面的内容</p>
</div>
</template>
```
至此,我们实现了一个简单的Tab栏切换案例,根据不同的子路由切换展示不同的页面内容。
阅读全文