03 Vue3路由
Vue3的路由与Vue2相似,关于 Vue2的路由器相关可以参考
Vue2 vue-router
下面是一些补充
路由器工作模式
- 
history模式优点: URL更加美观,不带有#,更接近传统的网站URL。缺点:后期项目上线,需要服务端配合处理路径问题,否则刷新会有 404错误。const router = createRouter({ history:createWebHistory(), //history模式 /******/ })
- 
hash模式优点:兼容性更好,因为不需要服务器端处理路径。 缺点: URL带有#不太美观,且在SEO优化方面相对较差。const router = createRouter({ history:createWebHashHistory(), //hash模式 /******/ })
命名路由
可以简化路由跳转及传参
给路由规则命名:
routes:[
  {
    name:'zhuye',
    path:'/home',
    // 设置重定向
    redirect:'/',
    component:Home
  },
  {
    name:'xinwen',
    path:'/news',
    component:News,
  },
  {
    name:'guanyu',
    path:'/about',
    component:About
  }
]
to的两种写法
字符串写法
<router-link active-class="active" to="/home">主页</router-link>
active-class 是自定义活动类的名称
对象写法
# 可以写路径
<router-link active-class="active" :to="{path:'/home'}">Home</router-link>
# 也可以写路由的名称
<router-link active-class="active" :to="{name:'zhuye'}">Home</router-link>
路由传参
query参数
传递参数
     <!-- 跳转并携带query参数(to的字符串写法) -->
      <router-link to="/news/detail?a=1&b=2&content=欢迎你">
      	跳转
      </router-link>
      <!-- 跳转并携带query参数(to的对象写法) -->
      <RouterLink 
        :to="{
          //name:'xiang', //用name也可以跳转
          path:'/news/detail',
          query:{
            id:news.id,
            title:news.title,
            content:news.content
          }
        }"
      >
        {{news.title}}
      </RouterLink>
接收参数-【route.query】
  ```js
  import {useRoute} from 'vue-router'
  const route = useRoute()
  // 打印query参数
  console.log(route.query)
  ```
params参数
一定要设置路由占位
路由规则占位
{
	name:'xiang',
	// 设置占位符,?表示该参数可传可不传
	path:'detail/:id/:title/:content?',
	component:Detail,
}
传递参数
     <!-- 跳转并携带params参数(to的字符串写法),不用加&连接,直接写内容即可 -->
      <RouterLink :to="`/news/detail/001/新闻001/内容001`">{{news.title}}</RouterLink>
      <!-- 跳转并携带params参数(to的对象写法) -->
      <RouterLink 
        :to="{
          name:'xiang', // 必须用name跳转,不可以使用path
          params:{
            id:news.id,
            title:news.title,
            content:news.title
          }
        }"
      >
        {{news.title}}
      </RouterLink>
接收参数-【route.params】
     import {useRoute} from 'vue-router'
     const route = useRoute()
     // 打印params参数
     console.log(route.params)
备注1:传递
params参数时,若使用to的对象写法,必须使用name配置项,不能用path。备注2:传递
params参数时,需要提前在规则中占位。
路由的props配置--【推荐】
作用:让路由组件更方便的收到参数(可以将路由参数作为props传给组件),直接引用使用
{
	name:'xiang',
	path:'detail',
	component:Detail,
  // props的布尔值写法,作用:把收到了每一组【params参数】,作为props传给Detail组件--【主要params传参】
  // props:true
  
  // props的函数写法,作用:把返回的对象中每一组key-value作为props传给Detail组件
  props(route){
    return route.query
  }
}
接收参数的时候,直接接收参数,并使用
<script lang="ts" setup>
defineProps(['id', 'title', 'content']);
</script>
replace属性
- 
作用:控制路由跳转时操作浏览器历史记录的模式。 
- 
浏览器的历史记录有两种写入方式:分别为 push和replace:- push是追加历史记录(默认值)
- replace是 替换当前记录
 
- 
开启 replace模式:<RouterLink replace .......>News</RouterLink>
编程式导航
路由组件的两个重要的属性:$route和$router变成了两个hooks
import {useRoute,useRouter} from 'vue-router'
const route = useRoute()
const router = useRouter()
// 接收参数
console.log(route.query)
console.log(route.parmas)
// 路由跳转
console.log(router.push)
console.log(router.replace)
// 参考
router.push('/news')
router.push({
  path:'/news',
  query:{
    a:XXX
    b:xxx
  }
})
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号