通过Vue路由实现Tab栏切换案例,要求为:创建3个子路由,分别是“待付款”、“待发货”、“待收货”,在每个子路由页面单独写出相应的内容,页面效果如下

原题目Vue路由课后习题_使用vue路由实现tab栏切换案例待付款-CSDN博客,我是使用了的另一种方法。

  1. Vue-router 是什么?它有哪些组件?

Vue-router 是 Vue.js 官方的路由管理器。它和 Vue.js 深度集成,使构建单页面应用变得易如反掌。Vue-router 的主要作用是构建单页应用(SPA)的路由系统,可以方便的将组件映射到路由上,使得我们可以控制组件的渲染和展示。

Vue-router 的主要组件包括:

  • <router-link>:用于导航链接,它会被渲染为一个 <a> 标签,点击时导航到对应的路由。
  • <router-view>:路由出口,路由匹配到的组件将渲染在这里。
  • router:VueRouter 的实例,通常我们在 Vue 组件中通过 this.$router 访问它,用于编程式导航。
  • route:当前路由对象,是一个包含了当前 URL 解析得到的信息的对象,可以通过 this.$route 在组件内部访问。
  1. 什么是嵌套路由?

嵌套路由就是路由中的路由,即路由可以嵌套使用。在 Vue-router 中,我们可以通过在路由配置中使用 children 属性来定义嵌套路由。嵌套路由常用于构建复杂的 UI 界面,比如一个用户信息页面可能包含用户的基本信息、联系方式等多个子页面。

  1. 路由如何传递参数?

在 Vue-router 中,传递参数主要有两种方式:

  • 通过 URL 的 query 参数传递:通过在 URL 后面添加查询字符串来传递参数,例如 /user?id=123。在 Vue 组件中,可以通过 this.$route.query 来获取这些参数。
  • 通过 URL 的 path 参数传递:在路由配置中,可以通过定义带参数的路由路径来接收参数,例如 /user/:id。在 Vue 组件中,可以通过 this.$route.params 来获取这些参数。

1.首先使用vuex创建状态管理库store.js

// store.js
// 用Vuex或其他状态管理库来管理全局状态。
import { createStore } from 'vuex'

export default createStore({
    state: {
        orderStatus: {
            id: 0,
            name: '待付款'
        }
    },
    mutations: {
        updateOrderStatus(state, payload) {
            // 将传入的payload对象的id属性赋值给state对象中的orderStatus的id属性
            state.orderStatus.id = payload.id
            // 将传入的payload对象的name属性赋值给state对象中的orderStatus的name属性
            state.orderStatus.name = payload.name
        }
    },
    actions: {
        setOrderStatus({ commit }, payload) {
            // 调用commit方法,更新订单状态
            commit('updateOrderStatus', payload)
        }
    },
    getters: {
        orderStatus: state => state.orderStatus
    }
})

2.在main.js中创建vue应用并使用store

import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import GlobalComponentA from './components/GlobalComponentA.vue'
// 在的主入口文件(如main.js或main.ts)中,你需要创建Vue应用并使用这个store:
import store from './views/order/store.js'

const app = createApp(App)

app.use(store)
app.use(router)
// 注册全局组件, 组件名: 组件对象
app.component('GlobalComponentA', GlobalComponentA)

app.mount('#app')

3.在页面中使用store

<script setup>

import { computed, nextTick } from 'vue'
import { useStore } from 'vuex'
import { onMounted } from 'vue';
const store = useStore()
onMounted(() => {
    console.log(store.getters.orderStatus);
})

// 使用computed来获取订单状态,以确保它是响应式的
const orderStatus = computed(() => store.getters.orderStatus)

const changeStatus = async () => {
    if (orderStatus.value.id === 0) {
        store.dispatch('setOrderStatus', { id: 1, name: '已付款' }),
            await nextTick();

    }
    console.log(store.getters.orderStatus);
}

</script>

<template>
    <div>
        <h1>商品状态为:{{ orderStatus.name }}</h1>
        <input type="button" value="付款" @click="changeStatus">
    </div>
</template>

<style scoped>
/* 你的样式 */
</style>

4.同理其他路由页面只需修改id值就能实现状态的改变。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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切换案例,根据不同的路由切换展示不同的页面内容

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值