1.安装router

npm install vue-router

2.为了方便管理在components同级创建router文件夹

3.在文件夹中创建index.js文件,就是router文件

import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
Vue.use(Router)
const router = new Router({
routes
})
router.beforeEach((to,from,next)=>{
if(to.matched.some((route)=>route.meta.Auth)){
next({
path:'/login',
query:{
returnURL:to.path
}
})
}else{
next()
} })
export default router

4.在这里是把routes分离开,作为一个模块进行定义,在index.js中引入,这样可以使模块更加的细化

import Artical from '../views/Artical'
import Channel from '../views/Channel'
import Index from '../views/Index'
import Setting from '../views/Setting'
import Base from '../views/setting/Base'
import Password from '../views/setting/Password'
import Avatar from '../views/setting/Avatar'
import Login from '../views/Login'
const routes = [
{
path: '/',
component: Index,
redirect:'/artical/1',
children: [
{
path: 'artical/:id',
component: Artical
},
{
path: 'channel/:id',
component: Channel
},
{
path:'setting',
component:Setting,
meta:{
Auth:true
},
redirect:'/setting/base',
children:[
{
path:'base',
component:Base
},
{
path:'password',
component:Password
},
{
path:'avatar',
component:Avatar
}
]
} ]
},{
path:'/login',
component:Login
}
]
export default routes

5.其他文件使用的时候,在routes.js中进行定义就可以啦

vue的cli中自定义router的更多相关文章

  1. vue的cli中引入css文件

    在public文件中创建一个文件夹css,放进reset.css 在main.js中引入即可 import '../public/css/reset.css'就可以啦

  2. vue 在 html 中自定义 tag

    v-if,v-for,:key,:style,v-text,@click,:src,:poster,:class,:href

  3. 【面试题】Vue中的$router 和 $route的区别

    Vue中的$router 和 $route的区别 点击视频讲解更加详细 this.$route:当前激活的路由的信息对象.每个对象都是局部的,可以获取当前路由的 path, name, params, ...

  4. vue cli 中关于vue.config.js中chainWebpack的配置

    Vue CLI  的官方文档上写:调整webpack配置最简单的方式就是在vue.config.js中的configureWebpack选项提供一个对象. Vue CLI 内部的 webpack 配置 ...

  5. vue中自定义组件(插件)

    vue中自定义组件(插件) 原创 2017年01月04日 22:46:43 标签: 插件 在vue项目中,可以自定义组件像vue-resource一样使用Vue.use()方法来使用,具体实现方法: ...

  6. vue中自定义指令vue.direvtive,自定义过滤器vue.filter(),vue过渡transition

    自定义指令 默认设置的核心指令( v-model,v-bind,v-for,v-if,v-on等 ),Vue 也允许注册自定义指令.注意,在 Vue2.0 里面,代码复用的主要形式和抽象是组件——然而 ...

  7. vue中自定义指令

    //vue中自定义指令 //使用 Vue.directive(id, [definition]) 定义全局的指令 //参数1:指令的名称.注意,在定义的时候,指令的名称前面,不需要加 v-前缀; 但是 ...

  8. Vue中自定义指令的使用方法!

    除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令.注意,在 Vue2.0 中,代码复用和抽象的主要形式是组件.然而,有的情况下,你仍然需要对普通 DOM ...

  9. vue cli中的env详解

    前言 相信使用过 vueCli 开发项目的小伙伴有点郁闷,正常开发时会有三个接口环境(开发,测试,正式),但是 vueCli 只提供了两种 development,production(不包含 tes ...

随机推荐

  1. Django的rest_framework的序列化组件之序列化多表字段的方法

    首先,因为我们安装了restframework,所以我们需要在django的settings中引入restframework INSTALLED_APPS = [ 'django.contrib.ad ...

  2. PHP 依赖工具 monolog的使用

    <?phprequire 'vendor/autoload.php'; //自动加载类库 use Monolog\Logger;use Monolog\Handler\StreamHandler ...

  3. three.map.control

    网址:https://github.com/anvaka/three.map.control 在threejs群里发现的一个很有意思的问题之前没有接触过: 存在的问题:  我在微信小游戏中,用orbi ...

  4. Excel上传找到错误数据类型

    一:查询数据库表中字段的类型语句 SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名, col.colorder AS ...

  5. vue vue-route 传参 $route.params

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. MFC 一个无参线程的CreateThread 使用

    最近想把c#的一个工作中用到的软件用MFC 实现出来, 刚下手 要了解的东西挺多,不但要对c++的语法,大体看一遍. 还要看MFC 内一些窗体,之类的相关的定义,比如cpp ,.h 内的类的定义方式等 ...

  7. RDMA的基础概念

    一张图可以简单明确的说明,目前RDMA的几种技术的差别: RDMA是remote Direct memory access的简称,有几个最基本的特点: CPU offload kernel bypas ...

  8. linux系统web站点设置-http基础设置

    一.httpd2.2的组成: /etc/httpd:服务器的根目录 conf/httpd.conf,conf.d/*:配置文件 conf/magic:MIME的配置文件 logs:日志文件的存放路径, ...

  9. python httplib2应用get post

    import httplib2,time #装饰器方法,用于记录方法消耗时间 #推荐将print 改成log def timer(func):     def _warpper(self,*argv) ...

  10. Linux 终端设备

    <Linux终端设备详解> https://www.cnblogs.com/shineshqw/articles/2423989.html