import VueRouter from 'vue-router'
//引入组件
import About from '../components/117/About.vue'
import Home from '../components/117/Home.vue'
import News from '../components/117/News.vue'
import Message from '../components/117/Message.vue'
import Detail from '../components/117/Detail.vue'
//创建路由器
const router = new VueRouter({
mode:'history',//影响路由中的效果,默认值是hash,路由会有#号,history则没有但兼容性差一些,上线后url会被当成资源导致404错误,http://localhost:8080/#/home/
/*
注意这里是routes不是routers不要写错了,
否则router-view会失效,且控制台会报No routes found in router
*/
routes:[
{
path:'/about',//路由
component:About,//组件名
meta:{title:'关于我'},
//独享路由守卫
beforeEnter:(to,from,next)=>{
//可以加一些逻辑到这里
next()
}
},
{
path:'/home',//路由
component:Home,//组件名
meta:{title:'首页'},
children:[
{
path:'news',//对应home/news
component:News,
meta:{title:'新闻'},
},
{
path:'message',
component:Message,
meta:{title:'消息'},
children:[
//query传参方式:?id=xx&title=yyy
{
name:'xiangqing',//路由命名
path:'detail',
component:Detail,
//props的第三种写法,值为函数
props($route){
return {
id:$route.query.id,
title:$route.query.title
}
},
//是否要验证权限
meta:{title:'详情',isAuth:true}
}
//params传参方式:id/xx/title/yyy
// {
// name:'xiangqing',//路由命名,params方式必须用name方式
// path:'detail/:id/:title',
// component:Detail,
// //props的第一种写法,什为对象,该对象中的所有key-value都会以props的形式传给detail组件
// //props:{a:1,b:'hello'}
// //props的第二种写法,值为布尔值,若为true,就会把该路由组件收到的所有params参数,以props的形式传给detail组件
// //props:true,//注意,这种只能适用params传参形式,不能适用query传参
// }
]
}
]
},
]
})
//全局前端路由守卫
//路由切换之前被调用,可以用来做权限控制或设置页面title等
router.beforeEach((to,from,next)=>{
//设置页面title
document.title=to.meta.title
// if(localStorage.getItem('xxxx')==='xxx'){
//如果有很多路由要判断就不不好写了,在路由配置上加meta:{isAuth:false或true}
//if(to.path==='/home/news'||to.path==='/home/message'){
//if(!to.meta.isAuth){
next()//放行
//}
})
//全局后置路由,初始化时执行、每次路由切换之后被调用
router.afterEach((to,from)=>{
})
export default router
- 相关文章
- 初识vue
- 数据绑定:第一个vue例子
- vue模板语法
- vue数据绑定
- el与data的两种写法/延迟挂载
- vue数据代理-Object.definePropert
- vue事件及事件修饰符prevent/stop/
- vue计算属性computed
- vue(深度)监视属性watch
- vue绑定样式:class,:style
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- laravel页面静态化的方法
- centos7.4 64位下swoole安装及配置
- navicate连接mycat报1184错误
- curl设置超时不起作用(CURLOPT_TIM
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:623篇
- 浏览数:1303109