Pinia+Router学习笔记(八)

简介: 本节记录Vue中命名路由-编程式导航相关内容
命名路由的作用:简化路由跳转,无需使用path硬编码。除了router-link外其实我们也可以使用a标签和href属性来进行跳转。但如此做会造成页面刷新(闪了一下),所以我们还是建议使用router-link

router中的定义方式如下:

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
    // 命名路由写法
        name: 'Login',
        component: () => import('../components/login.vue'),
    },
    {
    // 非命名路由写法
        path: '/reg',
        component: () => import('../components/reg.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

在App.vue中使用

<template>
  <h1>小满最骚</h1>
  // 注意:若要使用命名路由跳转方式则to中的内容必须是一个对象(即必须借助v-bind进行绑定)
  <router-link :to="{ name: 'Login' }">点击此处跳转至Login</router-link>
  <br>
  <router-link to="/reg">点击此处跳转至Reg</router-link>
  <router-view></router-view>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
</script>

<style scoped></style>
编程式导航:无需router-link标签即可实现跳转、传参等操作,通过在脚本中使用useRouter并调用API实现

字符串模式:

<template>
  <h1>小满最骚</h1>
  <button @click="toPage('/')">Login</button>
  <br />
  <button @click="toPage('/reg')">Reg</button>
  <router-view></router-view>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  const router = useRouter()
  const toPage = (url: string) => {
    router.push(url)
  }
</script>

<style scoped></style>

无需多余的操作,直接push到url对应的地址,但要注意url必须是path

对象模式

<template>
  <h1>小满最骚</h1>
  <button @click="toPage('/')">Login</button>
  <br />
  <button @click="toPage('/reg')">Reg</button>
  <router-view></router-view>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  const router = useRouter()
  const toPage = (url: string) => {
    router.push({
      path: url,
    })
  }
</script>

<style scoped></style>

将router.push()的内容由字符串改为配置对象,path对应的url即为要跳转到的url

命名式

<template>
    <h1>小满最骚</h1>
    <button @click="toPage('Login')">Login</button>
    <br />
    <button @click="toPage('Reg')">Reg</button>
    <router-view></router-view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const toPage = (name: string) => {
    router.push({
        name,
    })
}
</script>

<style scoped></style>

在router.push配置对象中把path改为name,需要在index.ts里指定路由的名字

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    // 命名路由
    name: 'Login',
    component: () => import('../components/login.vue'),
  },
  {
    path: '/reg',
    name: 'Reg',
    component: () => import('../components/reg.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export default router
相关文章
|
9月前
|
缓存 JavaScript
Vue Router 学习 new Router
Vue Router 学习 new Router
85 0
|
1月前
|
缓存 移动开发 JavaScript
【学习笔记】Vue Router
【学习笔记】Vue Router
36 0
|
1月前
|
前端开发 JavaScript API
React Router v6 完全指南(下)
React Router v6 完全指南(下)
101 0
|
1月前
|
存储 资源调度 前端开发
React Router v6 完全指南(上)
React Router v6 完全指南(上)
193 0
|
移动开发 JavaScript 前端开发
React-Router
React-Router
54 0
|
网络架构
Pinia+Router学习笔记(十)
本节记录Vue-Router的两种路由传参方式
140 0
Pinia+Router学习笔记(三)
本节记录解构Store过程中的相关操作及注意事项
67 0
|
API
Pinia+Router学习笔记(五)
本节记录例API和Pinia持久化插件相关内容
68 0
|
前端开发 中间件 SEO
Pinia+Router学习笔记(七)
本节介绍Vue-Router的两种路由模式
68 0
|
JavaScript
Pinia+Router学习笔记(一)
本系列笔记内容根据B站up主“小满zs”视频教程整理而成,本节记录pinia的搭建过程
122 0

热门文章

最新文章