vue-router4使用

15694次阅读 1194人点赞 作者: WuBin 发布时间: 2021-02-23 13:15:41
扫码到手机查看

目录结构

app>src>router>index.js

              >main.js

              >App.vue

router/index.js

书写配置文件(只有两个组件的路由)注意,关于数据获取,最好在每个路由中进行分别获取!

import { createRouter, createWebHashHistory } from 'vue-router';
import Detail from '../components/detail/detail';
import MainList from '../components/main-list/main-list';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: MainList,
    },
    {
      path: '/detail',
      name: 'detail',
      component: Detail,
    }
  ]
});

export default router;

在main.js中引用

import { createApp } from 'vue';
import App from './App.vue';
import router from './router/index';
import fastclick from 'fastclick';
import VConsole from 'vconsole';
import 'common/less/index.less';
fastclick.attach(document.body);
let vConsole = new VConsole();

createApp(App).use(router).use(vConsole).mount('#app');

vue-router4安装

npm install vue-router@4
或者
<script src="https://unpkg.com/vue-router@4"></script>

在App.vue中使用

<router-view v-slot="{ Component }">
      <transition name="aside-right" appear>
          <component :is="Component" />
      </transition>
</router-view>
// 跳转到路由
this.$router.push({
        name: 'detail',
        query: {
          target: item.target
        }
})

使用params传递参数:

// 静态引用会在首屏加载时候载入全部组件,所以这里路由应该改为动态加载
import { createRouter, createWebHashHistory } from 'vue-router'
import ViewHome from '@/views/home/home';
import ViewEdit from '@/views/edit/edit';

// 路由配置
const routes = [
    // 配置当访问根路径/的时候,跳转到/recommend的路径 渲染Recommend组件
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: ViewHome,
        children: []
    },
    {
        path: '/edit/:id',
        name: 'edit',
        component: ViewEdit,
        // 指定子路由 每个对象都是子路由配置
        children: []
    }
];

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

export default routers

使用name+params传递:

openEdit(id, index) {
        this.$router.push({
             name: 'edit',
             params: {
                  id: id
              }
         });
},

使用path:

openEdit(id, index) {
       this.$router.push({
            path: `/edit/${id}`
       });
},

v-slot="{ Component } 中的Component就是路由中定义的指向的组件!即本例中的MainList和Detail!

关于使用时的问题,以及给路由的组件添加过度动画,请参见 vue-router4过度动画无效解决方案

如果要在router-view中使用路由元信息,比如实现每个路由有不同的过度

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'mainlist',
      component: MainList,
      // 路由元信息定义每个路由进入退出的不同动画
      meta: { transition: 'fadeIN' }
    },
    {
      path: '/detail',
      name: 'detail',
      component: Detail,
      meta: { transition: 'aside-right' }
    }
  ],
<!-- 这里Component就是指的router中注册的组件, route就是每个路由的数组routeS -->
    <router-view v-slot="{ Component, route }">
     // 动态读取绑定路由元信息,v-slot = {route}就是配置中的routes数组
      <transition :name="route.meta.transition" appear>
        <!--  加入keep-alive保证进入退出都有动画 -->
        <!--  exclude排除缓存,让其不缓存数据 -->
        <keep-alive exclude="detail">
          <component :is="Component" 
                   @closeDetail="监听组件派发的特定事件" 
                   :all="给路由组件传递的数据"
           />
        </keep-alive>
      </transition>
    </router-view>

注意上面的exclude="detail"中的detail指的是组件的名称!,并非是路由中的name!比如:

<router-view v-slot="{ Component, route }">
      <transition :name="route.meta.transition"
                  appear
      >
        <keep-alive exclude="video-detail">
          <component :is="Component"></component>
        </keep-alive>
      </transition>
</router-view>

其中的video-detail指的是video-detail组件中的name!

export default {
  name: "video-detail",
  data() {。。。
},

并不是router/index.js 配置文件中的路由名称!

routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
    },
    {
      path: '/detail/:id',
      name: 'detail', // 排除的名称不能以路由配置文件中的为准!!
      component: VideoDetail,
      meta: { transition: 'aside-right' }
    }
  ],

exexclude="video-detail"之后,vue就不会缓存该组件,每次进入的时候都会执行created()钩子。

相关资料

点赞 支持一下 觉得不错?客官您就稍微鼓励一下吧!
关键词:vue-router安装配置
推荐阅读
  • uniapp实现被浏览器唤起的功能

    当用户打开h5链接时候,点击打开app若用户在已经安装过app的情况下直接打开app,若未安装过跳到应用市场下载安装这个功能在实现上主要分为两种场景,从普通浏览器唤醒以及从微信唤醒。

    8757次阅读 562人点赞 发布时间: 2022-12-14 16:34:53 立即查看
  • Vue

    盘点Vue2和Vue3的10种组件通信方式

    Vue中组件通信方式有很多,其中Vue2和Vue3实现起来也会有很多差异;本文将通过选项式API组合式API以及setup三种不同实现方式全面介绍Vue2和Vue3的组件通信方式。

    3674次阅读 274人点赞 发布时间: 2022-08-19 09:40:16 立即查看
  • JS

    几个高级前端常用的API

    推荐4个前端开发中常用的高端API,分别是MutationObserver、IntersectionObserver、getComputedstyle、getBoundingClientRect、requ...

    13775次阅读 888人点赞 发布时间: 2021-11-11 09:39:54 立即查看
  • PHP

    【正则】一些常用的正则表达式总结

    在日常开发中,正则表达式是非常有用的,正则表达式在每个语言中都是可以使用的,他就跟JSON一样,是通用的。了解一些常用的正则表达式,能大大提高你的工作效率。

    12532次阅读 422人点赞 发布时间: 2021-10-09 15:58:58 立即查看
  • 【中文】免费可商用字体下载与考证

    65款免费、可商用、无任何限制中文字体打包下载,这些字体都是经过长期验证,经得住市场考验的,让您规避被无良厂商起诉的风险。

    11038次阅读 881人点赞 发布时间: 2021-07-05 15:28:45 立即查看
  • Vue

    Vue3开发一个v-loading的自定义指令

    在vue3中实现一个自定义的指令,有助于我们简化开发,简化复用,通过一个指令的调用即可实现一些可高度复用的交互。

    15139次阅读 1207人点赞 发布时间: 2021-07-02 15:58:35 立即查看
  • JS

    关于手机上滚动穿透问题的解决

    当页面出现浮层的时候,滑动浮层的内容,正常情况下预期应该是浮层下边的内容不会滚动;然而事实并非如此。在PC上使用css即可解决,但是在手机端,情况就变的比较复杂,就需要禁止触摸事件才可以。

    14612次阅读 1191人点赞 发布时间: 2021-05-31 09:25:50 立即查看
  • Vue

    Vue+html2canvas截图空白的问题

    在使用vue做信网单页专题时,有海报生成的功能,这里推荐2个插件:一个是html2canvas,构造好DOM然后转canvas进行截图;另外使用vue-canvas-poster(这个截止到2021年3月...

    28356次阅读 2230人点赞 发布时间: 2021-03-02 09:04:51 立即查看
  • Vue

    vue-router4过度动画无效解决方案

    在初次使用vue3+vue-router4时候,先后遇到了过度动画transition进入和退出分别无效的情况,搜遍百度没没找到合适解决方法,包括vue-route4有一些API都进行了变化,以前的一些操...

    24476次阅读 1885人点赞 发布时间: 2021-02-23 13:37:20 立即查看
交流 收藏 目录