Vue3中路由的相关配置,跳转,传参,嵌套

Vue3中路由的相关配置
1、路由的配置

在src路径下新建router文件夹,然后在该文件夹新建index.js文件

安装vue-router

cnpm i vue-router@next --save

index.js文件配置如下。

import { createRouter, createWebHistory } from "vue-router";

const addCount = () => import('../components/addCount.vue')
const Father = () => import('../components/father.vue')


const routes = [
    {
        path: "/",
        redirect: '/add'
    },
    {
        path: "/father",
        component: Father,
    },
    {
        path: "/add",
        component: addCount
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: routes
})
export default router


然后配置main.js 引入router

import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
//使用router
//引入elementPlus
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
//引入router
import router from './router/index'

const app = createApp(App)
//使用ElementPlus
app.use(ElementPlus)
//使用router
app.use(router)

app.mount('#app')
2、路由的跳转

方式一

引入useRouter

<template>
  <div>
    <el-button type="primary" @click="changeRouter"> 跳转路由 </el-button>
  </div>
</template>

<script>
import { useRouter} from 'vue-router'
export default {
  setup() {
    const router = useRouter()
    const changeRouter = () => {
      router.push('/father')
    }
    return { changeRouter }
  }
}
</script>

<style  scoped>
</style>

方式二router-link

<router-link to="/child">子页面</router-link>

方式三

<template>
  <div>
      <el-button type="primary" @click="toAdd">下一页</el-button>
  </div>
</template>

<script>
import router from '../router/index.js'
export default {
  methods: {
    toAdd() {
      router.push('/add')
    },
  },
}
</script>

<style scoped>

</style>
3、路由的传参

引入useRoute

<template>
  <div>
    <el-button type="primary" @click="changeRouter"> 跳转路由 </el-button>
  </div>
</template>

<script>
import { useRouter, useRoute } from 'vue-router'
export default {
  setup() {
    const router = useRouter()
    
    const route = useRoute()

    const changeRouter = () => {
    router.push({
        path: '/father',
        //传递的参数
        query: {
          name: 'zz',
        },
      })
    }

    return { changeRouter }
  },

}
</script>

<style  scoped>
</style>

如下图:father页面就可以通过route.query.name获取到"zz"

6saqCn.png

4、路由的嵌套

通过router-view和子路由的方式嵌套

router/index.js

import { createRouter, createWebHistory } from "vue-router";

const Father = () => import('../components/father.vue')
const children = () => import('../components/child.vue')

const routes = [
    {
        path: "/",
        redirect: '/father'
    },
    {
        path: "/father",
        component: Father,
        children:
            [
                {
                    path: '/child',
                    component: children
                }
            ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: routes
})
export default router


father.vue

<template>
  <div>
    <h1 class="h">我是爷爷</h1>
    <br />
    <router-link to="/child">下一页</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
}
</script>
<style scoped>

</style>

child.vue

<template>
  <div>
    <div>
      <hr>
      <h1>我是儿子</h1>
    </div>
    <Sunzi />
  </div>
</template>

<script>
import Sunzi from './sunzi.vue'
export default {
  components: {
    Sunzi,
  },
}
</script>

<style scoped>
</style>

运行如下图所示:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值