首页 > 其他分享 >Vue3 路由(上)

Vue3 路由(上)

时间:2024-10-14 23:18:08浏览次数:3  
标签:name title Vue3 query path news 路由

路由器两种模式

  1. history模式:
const router = CreateRouter({
  history:createWebHistory(), //history模式
})
  • 优点:url更加美观,不带有#,更接近传统网站的url
  • 缺点:后期项目上限,需要配合服务端处理路径问题,否则刷新会404错误
  1. hash模式:
const router = CreateRouter({
  history:createWebHashHistory(),  //hash模式
})
  • 优点:兼容性更好,不需要服务器端处理路径
  • 缺点:URL带有#不美观,但在SEO优化方面相对较差

to的两种写法

  1. 字符串写法
  <RouterLink to="/home" active-class="active">首页</RouterLink>
  <RouterLink to="/news" active-class="active">新闻</RouterLink>
  <RouterLink to="/about" active-class="active">关于</RouterLink>
  1. 对象写法
  <RouterLink to="/home" active-class="active">首页</RouterLink>
  <RouterLink to="/news" active-class="active">新闻</RouterLink>
  <RouterLink :to="{path:'/about'}" active-class="active">关于</RouterLink>

命名路由

const router = createRouter({
    // 路由器的工作模式:history模式
    history: createWebHistory(),
    // 路由,一个一个的路由规则
    routes: [
        {
            name: 'zhuye'
            path: '/home',
            component: Home
        },
        {
            name: 'guanyu'
            path: '/about',
            component: About
        },
        {
            name: 'guanyu'
            path: '/news',
            component: News
        }
    ]
})
 <RouterLink to="/home" active-class="active">首页</RouterLink>
 <RouterLink :to="{name:'/xinwen'}" active-class="active">新闻</RouterLink>
 <RouterLink :to="{path:'/about'}" active-class="active">关于</RouterLink>

tips:还可以通过名字跳转

嵌套路由

  1. 在index.ts中
const router = createRouter({
    // 路由器的工作模式:history模式
    history: createWebHistory(),
    // 路由,一个一个的路由规则
    routes: [
        {
            name:'zhuye',
            path: '/home',
            component: Home
        },
        {
            name:'guanyu',
            path: '/about',
            component: About
        },
        {
            name:'xinwen',
            path: '/news',
            component: News,
            children:[
                {
                    path:'detail',
                    component:Detail
                }
            ]
        }
    ]
})

tips:这里定义的path不用在前面添加/

  1. 在的news.vue中

  1. 在RouterLink中to属性中的路径要带上上一级的路径

query参数

  1. 传递参数
  • 第一种写法
<RouterLink :to="`/news/detail?id=${news.id}&title=${news.title}&content=${news.content}`">
  {{news.title}}
</RouterLink>

tips:

  1. :to:Vue 的 v-bind 语法简写形式,用于将 to 属性绑定到动态的值。to 指定了要跳转的路由目的地。在这个例子中,它使用的是 模板字符串 来动态构建 URL。
  2. 模板字符串:使用反引号(``)包裹内容的字符串模板,可以通过 ${} 的方式将变量嵌入到字符串中。这里是通过模板字符串动态构造 URL。
  • 第二种写法
       <Router-link
            :to="{
              name:'xiangxi',
              // path: '/news/detail',
              query:{
                id:news.id,
                title:news.title,
                content: news.content,
              }
            }">{{news.title}}</Router-link>

使用对象形式配置

  1. 接受参数
<template>
  <ul class="news-list">
    <li>编号:{{query.id}}</li>
    <li>标题:{{query.title}}</li>
    <li>内容:{{query.content}}</li>
  </ul>
</template>

<script setup lang="ts" name="About">
import {useRoute} from "vue-router";
import {toRefs} from "vue";
// 接受query传的参数
let route = useRoute();
// 把传递过来的参数变成响应式对象
const {query} = toRefs(route);
</script>

tips:

  1. 引入useRoute
  2. route中的query对象中包含传递的参数

params参数

  1. 在路由配置里面的路径要进行占位

  1. 传递参数
  • 第一种写法
 <!--2. params传参-->
 <RouterLink :to="`/news/detail/${news.id}/${news.title}/${news.content}`">
      {{news.title}}
 </RouterLink>

tips:

  1. 与query传参不同的是,使用params传参时,不需用&分割参数,直接使用/
  2. 同样需要使用模板字符串
  • 第二种写法
<RouterLink
	:to="{
	name: 'xiangxi',
	params:{
	  id:news.id,
		title:news.title,
		content:news.content,
		}

}">{{news.title}}
	</RouterLink>

tips:

  1. 不能使用path,使用name
  2. 不能传递对象或数组
  3. 占位有,但不传输的话,在占位时后面加?

标签:name,title,Vue3,query,path,news,路由
From: https://blog.csdn.net/Birdy_x/article/details/142931494

相关文章

  • 尚硅谷rabbitmq2024 工作模式路由篇 第11节 答疑
    StringexchangeName="test_direct";/!创建交换机人图全channel.exchangeDeclare(exchangeName,BuiltinExchangeType.DIREcT,b:true,b1:false,b2:false,map:null);/1创建队列Stringqueue1Name="test_direct_queue1";Stringqueue2Name="test......
  • nbsaas vue3管理后台框架
    nbsaasvue3管理后台框架一、项目概述NbsaasAdminVue是一个基于Vue.js3.0构建的轻量级后台管理系统,结合了现代前端技术栈的最佳实践,旨在帮助开发者快速构建具有高可扩展性和良好用户体验的后台管理系统。该项目拥有简洁的UI设计,强大的功能模块,支持多种自定义配置,......
  • vue3中的defineProps,defineEmits,defineExpose的使用详解
    defineProps 和 defineEmits 都是只能在 <scriptsetup> 中使用的编译器宏。他们不需要导入,且会随着 <scriptsetup> 的处理过程一同被编译掉。defineProps 接收与 props 选项相同的值,defineEmits 接收与 emits 选项相同的值,它们具备完整的类型推断并且在scriptse......
  • vue3 antvX6的使用源码
    npminstall--save@antv/x6<template><divclass="dashboard-container"><p>选择节点</p><button@click="save">保存</button><divclass="antvBox"><divclass="m......
  • vue3 做个点击拖拽的的按钮
    //视图层<divclass="regularAI"ref="draggableDiv":style="{top:`${position.y}px`,left:`${position.x}px`}"> <span@mousedown="startDrag">{{isDragging?'拖拽中':'点击拖拽'}}</span&......
  • adminPage-vue3依赖 v1.2.0新增组件 DetailsModule说明文档
    adminPage-vue3依赖v1.2.0新增组件DetailsModule说明文档引入思路介绍DetailsModuleAPI汇总属性插槽自定义对象config(array<object\>/object类型)config.list(array<object\>类型)使用基础使用范例config-titleconfig-moduleKeyconfig-listconfig-list-slotNamec......
  • 看不懂来打我!让性能提升56%的Vue3.5响应式重构
    前言在Vue3.5版本中最大的改动就是响应式重构,重构后性能竟然炸裂的提升了56%。之所以重构后的响应式性能提升幅度有这么大,主要还是归功于:双向链表和版本计数。这篇文章我们来讲讲使用双向链表后,Vue内部是如何实现依赖收集和依赖触发的。搞懂了这个之后你就能掌握Vue3.5重构后的响......
  • 通过路由守卫实现免登陆
    importrouterfrom'./router'importstorefrom'./store'import{Message}from'element-ui'importNProgressfrom'nprogress'//进度条import'nprogress/nprogress.css'//进度条样式import{getToken,setToken......
  • vue之间的传值问题---1路由传值router
    路由传值:query传参,刷新页面不会丢失参数。但是params会丢参的。通过路由参数传值:可以通过路由的动态参数或查询参数传递数据。在源页面设置参数,然后在目标页面通过$route.params或$route.query访问参数。1.query传参:query在浏览器地址栏中显示参数//路由{ path:'/test',......
  • vue-cropper图片裁剪(vue2与vue3)
    在项目中,前端开发经常会遇到有图片上传的需求,而别人的组件大多都满足不了当下产品的需求,这是往往我们得去依靠组件自己自定义一个项目通用的裁剪组件一、vue-cropper安装依赖:vue2:npminstallvue-cropper 或 yarnaddvue-croppervue3:npminstallvue-cropper@next 或......