美文网首页
Vue Router 使用实例

Vue Router 使用实例

作者: UULU | 来源:发表于2019-01-03 19:10 被阅读0次

在对Vue Router不熟练的情况下,很容易走弯路,过后才会发现原来还有捷径。我的原则是一个变量能解决的问题绝对不会用两个,如果不用自己定义变量是最好的。

下面是核心用法的总结,这个实例包含了以下这些知识点 (非完整实例,未给出无关的代码)

  • 懒加载组件的通用函数
  • 路径重定向
  • 子组件
  • 父组件的根路径组件
  • 组件的 key 属性
  • 解决带 params 路径跳转,页面不刷新的问题
  • 自定义 404 页面
  • 用数组初始化菜单
  • 根据当前网址 Path,将匹配的菜单项修改为激活状态

src/router.js

项目中直接使用了path跳转,所有未指定name属性

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 实现“懒加载组件”的通用函数
function component(comp) {
  return () =>
    import(/* webpackChunkName: "comp-[request]" */ `@/components/${comp}`)
}

function view(comp) {
  return () =>
    import(/* webpackChunkName: "view-[request]" */ `@/view/${comp}`)
}

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      component: loadComponent('Login')
    },
    {
      // '/home' 重定向到 `/`
      path: '/home',
      redirect: '/'
    },
    {
      path: '/',
      component: view('Home'),
      children: [
        {
          // 关联 `/` 根路径的组件
          path: '/',
          component: component('Main')
        },
        {
          // 关联 `/profile` 路径的子组件
          path: '/profile',
          component: component('home/Profile')
        },
        {
          // 关联 `/hero/{英雄ID}` 带params路径的组件
          path: '/hero/:heroId',
          component: component('Hero')
        }
      ]
    },
    {
      path: '/about',
      component: view('About')
    },
    {
      // 自定义 404 页面,否则会打开默认界面
      path: '*',
      component: view('NotFound')
    }
  ]
})

src/App.vue

<template>
  <div id="app"><router-view /></div>
</template>

src/components/Home.vue

组件的key属性可以唯一标记组件,如果不指定 ,不同的path指向同一个component,vue 会直接重用这个组件,而不会重新触发created()

<template>
  <div class="home">
    <div class="header">
      <ul>
        <!-- 遍历数组初始化菜单 -->
        <!-- 匹配当前path的菜单项,样式修改为 'menu-active' -->
        <li
          v-for="(item, index) in menus"
          :key="index"
          :class="item.path == $route.path? 'menu-active': 'menu'"
          @click="onClick(item)"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
    <div class="main">
      <!-- 用path绑定组件的Key,解决带params路径跳转,页面不刷新的问题 -->
      <router-view :key="$route.path"></router-view>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        menus: [
          {
            title: '首页',
            path: '/'
          },
          {
            title: '关于',
            path: '/about'
          }
        ]
      }
    },
    methods: {
      onClick(item) {
        this.$router.push(item.path)
      }
    }
  }
</script>

相关文章

  • Vue-Router核心实现原理

    如何在vue中使用vue-router? 1.Vue使用插件,即调用Vue的use方法; 2.实例化router,...

  • Vue.js 学习笔记(三)

    路由 vue-router 的基本使用 定义组件 定义 vue-router 关联到 Vue 实例 实现 效果展示...

  • Vue 基础 - 前端路由

    使用vue-router实现前端路由 安装vue-router: 配置路由文件,并在vue实例中导入: 设置视图(...

  • vue2-vue-router

    用法 使用vue-router插件 router.js 创建Router实例 router.js 在跟组件上添加实...

  • 利用vue-router做跳转优化

    当我们在用vue进行开发时,除了用vue-router配置路由,其次经常使用是router实例方法和

  • vue-router

    安装vue-router 引入 实例化router 将实例化的 router 添加到 vue 的实例中 在实例化r...

  • Vue Router Api参考(三)

    Router 实例属性 router.app 类型: Vue instance配置了 router 的 Vue 根...

  • vue-router使用小结

    使用vue-router必须注意两点: 一是vm实例$router(使用push发送跳转url)和跳转视图$rou...

  • VUE Router使用实例

    子路由规则 通过url传递参数 路由跳转方式 1、声明式导航:

  • Vue Router 使用实例

    在对Vue Router不熟练的情况下,很容易走弯路,过后才会发现原来还有捷径。我的原则是一个变量能解决的问题绝对...

网友评论

      本文标题:Vue Router 使用实例

      本文链接:https://www.haomeiwen.com/subject/ssuqrqtx.html