美文网首页
Vue 路由一

Vue 路由一

作者: 阿文灬 | 来源:发表于2019-10-13 20:02 被阅读0次

这里主要讲解开发过程 使用 路由 的思路,而不是讲解router的概念或知识点。关于知识点的讲解与详细内容可以查看官网
https://cn.vuejs.org/v2/guide/routing.html
https://router.vuejs.org/

  • 一个非常简单的路由
  • 使用vue-router管理路由
  • 在页面加载前后,添加功能和权限控制

一个非常简单的路由

如果你只需要非常简单的路由而不想引入一个功能完整的路由库,可以像这样动态渲染一个页面级的组件:

// main.js
import Vue from 'vue';

const routes = {
  '/': Home,
  '/about': About
};

const app = new Vue({
  el: '#app',
  data: {
    currentRoute: window.location.pathname
  },
  computed: {
    ViewComponent() {
      const matchingView = routes[this.currentRoute];
      return matchingView
        ? require('./views/pages/' + matchingView + '.vue')
        : require('./views/pages/404.vue');
    }
  },
  render(h) {
    return h(this.ViewComponent);
  }
});

window.addEventListener('popstate', () => {
  app.currentRoute = window.location.pathname;
});

使用vue-router管理路由

  • 添加 vue-router
npm i vue-router
  • 创建关于 router 的文件


    router.png
// router/index.js
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);

import { routes } from './routes';

const router = new Router({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes: routes
});

export default router;
// router.routes.js
export const routes = [
  {
    path: '/',
    name: 'basic-layout',
    component: () =>
      import(/* webpackChunkName: "layout" */ '../layouts/basic-layout'),
    children: [
      {
        path: '/',
        redirect: '/hello-world'
      },
      {
        path: '/hello-world',
        name: 'hello-world',
        component: () =>
          import(/* webpackChunkName: "hello-world" */ '../views/hello-world')
      },
      {
        path: '*',
        name: '404',
        component: () => import(/* webpackChunkName: "404" */ '../views/404')
      }
    ]
  }
];

  • 在 main.js 中Vue使用 router。当然 App.vue 也要做一些修改
// main.js
import Vue from 'vue';
import App from './App.vue';
import router from './router';

Vue.config.productionTip = false;

new Vue({
  render: h => h(App),
  router
}).$mount('#app');
// App.vue
<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

在页面加载前后,添加功能和权限控制

router.beforeEach 
router.afterEach

一般在页面加载前要 显示页面加载进度条判断是否登录判断页面的访问权限判断页面内部的操作权限等。

import Vue from 'vue';
import { checkAuthority, isLogin } from '../utils/auth';
import NProgress from 'nprogress';
import Router from 'vue-router';
Vue.use(Router);

import { routes } from './routes';

const router = new Router({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes: routes
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }

  if (isLogin()) {
    if (to.path === '/login') {
      next({ path: '/' });
    } else if (checkAuthority(to.path)) {
      next();
    } else {
      next({ path: '/403' });
    }
  } else {
    next({ path: '/user/login' });
  }
});

router.afterEach(transition => {
  NProgress.done(true);
});

export default router;

相关文章

  • Vue应用

    Vue项目 Vue结构 Vue项目打包与发布 Vue语法二 Vue网络请求 Vue路由 动态路由 编程式路由导航

  • vue路由、自定义指令、脚手架

    *Vue vue-router 一、路由 一、导航式路由 路由路径由

  • 手写 Vue Router、手写响应式实现、虚拟 DOM 和 D

    Vue-Router 原理实现 一、Vue-Router 动态路由 二、Vue-Router 嵌套路由 三、Vue...

  • Vue路由

    一、Vue路由基础用法: 二、Vue路由配置的抽出 三、路由动态传值: 四、路由的跳转方式: 五、路由的hash模...

  • 2018-09-19 vue 八

    一 :路由路由:vue-router是Vue的工具库 vue-router.js下载:npm install ...

  • Vue 路由

    一 :路由路由:vue-router是Vue的工具库 vue-router.js下载:npm install vu...

  • 2018-09-23 vue初学九(路由)

    路由 vue-router是Vue的工具库使用路由需要导入router库,和vue.js一起使用 路由的使用分为四...

  • Vue-router

    Vue路由详解 一、Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,...

  • 6 VUE路由

    vue-> SPA应用,单页面应用(引入vue-router.js) 路由嵌套(多层路由): 路由其他信息:

  • vue路由的介绍(二)--vue动态路由和get的传值

    vue动态路由和get的传值---->同属于路由的传参 1,vue动态路由: 动态路由的配置: ①,在配置路由时加...

网友评论

      本文标题:Vue 路由一

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