Vue学习计划-Vue3--核心语法(六)路由

1. 路由
  1. 【对路由的理解】
    Vue学习计划-Vue3--核心语法(六)路由_第1张图片

  2. 【基本切换效果】

  • Vue3中要使用vue-router的最新版本,目前是4版本
  • 路由配置文件代码如下:
// 创建一个路由器,并暴露出去
// 第一步:引入createRouter
import { createRouter, createWebHistory } from 'vue-router'
// 引入一个一个可能要呈现组件
import Home from '../components/Home.vue'
import News from '../components/News.vue'
import About from '../components/About.vue'
// 第二步:创建路由器并暴露出去
export default createRouter({
  history: createWebHistory(),//路由器的工作模式
  routes: [//一个一个的路由规则
    {
      path: '/home',
      component: Home
    },
    {
      path: '/news',
      component: News
    },
    {
      path: '/about',
      component: About
    }
  ]
})
  • main.ts代码如下:
// 引入createApp用于创建应用
import { createApp } from 'vue'
// 引入App根组件
import App from './App.vue'
// 引入路由器
import router from './router'
// 创建一个应用
const app = createApp(App)
// 使用路由器
app.use(router)
// 挂载整个应用到app容器中
app.mount('#app')
  • App.vue代码如下:



  1. 【两个注意点】
  • 路由组件通常存放在pagesviews文件夹,一般组件通常放在components文件夹
  • 通过点击导航,视觉效果上“消失”了的路由组件,默认是被卸载的,需要的时候再去挂载
  1. 【路由器工作模式】
  • history模式

优点:URL更加美观,不带有#,更接近传统的网站URL
缺点: 后期项目上线,需要服务端配合处理路径问题,否则刷新会有404错误

  const router = createRouter(
    history: createWebHistory(), // history模式
    ...
  )
  • hash模式

优点: 兼容性更好,因为不需要服务端处理路径
确定:URL带有#不美观,且在SEO优化方面相对较差

  const router = createRouter(
    history: createWebHashHistory(), // hash模式
    ...
  )
  1. to的两种写法】
新闻
关于
  1. 【命名路由】

    1. 作用:可以简化路由跳转及传参
    2. 给路由规则命名:
    {
      name: 'zhuye',
      path: '/home',
      component: Home
    },
    {
      name: 'xinwen',
      path: '/news',
      component: News
    },
    {
      name: 'guanyu',
      path: '/about',
      component: About
    }
    
    1. 跳转路由:
    新闻
    
  2. 【嵌套路由】

    1. 编写News的子路由:Detail.vue
    2. 配置路由规则,使用Children配置项
    export default createRouter({
      history: createWebHistory(),//路由器的工作模式
      routes: [//一个一个的路由规则
        {
          name: 'zhuye',
          path: '/home',
          component: Home
        },
        {
          name: 'xinwen',
          path: '/news',
          component: News,
          children: [
            {
              path: 'detail',
              component: Detail
            }
          ]
        },
        {
          name: 'guanyu',
          path: '/about',
          component: About
        }
      ]
    })
    
    1. 跳转路由(记得要加完整路径)与展示区
    • {{ news.title }}
  3. 【路由传参】

  • query参数
    1. 传递参数
     
      {{ news.title }}
      
      
      {{ news.title }}
    
    
    1. 接收参数
    
    
    
    
  • params参数
    1. 传递参数
    // 1. router文件中
    {
      name: 'xinwen',
      path: '/news',
      component: News,
      children: [
        {
          name: 'xiangqing',
          path: 'detail/:id/:title/:content',
          component: Detail
        }
      ]
    },
    // 2. news文件传递参数
      
      {{ news.title }}
    
      
      
      {{ news.title }}
    
    
    1. 接收参数
    
    
    
    

备注:

  1. 传递params参数时:若使用to的对象写法,必须使用name配置项,不能用path
  2. 传递params参数时:需要提前在规则中占位
  3. 传递params参数时:传递参数属性不能是对象类型
  4. 传递params参数时:若是非必传,占位后加?,如:path: 'detail/:id/:title?/:content?'
  1. 【路由的props配置】
  • 作用:让路由组件更方便的收到参数(可以将路由参数作为props传给组件)
// router中配置路由规则
{
  name: 'xiangqing',
  // path: 'detail/:id/:title/:content', // params传参
  path: 'detail',// query传参
  component: Detail,
  // 1. props的布尔值写法,作用:把收到了每一组params参数,作为props传给Detail组件
  // props: true
  //2.  props的函数写法,作用:把返回的对象中每一组key-value作为props传给Detail组件
  props(route){ // 参数route就是路由信息,可以解决query传参
    return route.query
  }
    //3.  props的对象写法,作用:把对象中每一组key-value作为props传给Detail组件
  //  props: { // 
  //   id: '111',
  //   title: '2222',
  //   content: '333'
  // }
}
// 接收参数



  1. replace属性】

    1. 作用:控制路由跳转时操作浏览器历史记录的模式
    2. 浏览器的历史记录有两种写入方式:分别是pushreplace:
    • push是追加历史记录(默认值)
    • replace是替换当前记录
    1. 开启replace模式:
      News
  2. 【编程式导航】
    路由组件的两个重要的属性: $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)
  1. 【重定向redirect
    1. 作用:将特定的路径,重新定向到已有路由
    2. 具体编码:
    // 不输入路径自动跳转
    {
      path: '/',
      redirect: '/about'
    },
    // 无法匹配重定向 随便输入路径,未匹配到做重定向
    {
      path: '/:pathMatch(.*)*',
      redirect: '/news'
    }
    

你可能感兴趣的:(#,vue3,学习路程,vue.js,学习,前端)