Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue路由管理 #3

Open
Alexandermclean opened this issue Mar 16, 2019 · 0 comments
Open

vue路由管理 #3

Alexandermclean opened this issue Mar 16, 2019 · 0 comments
Labels
Vue Vue

Comments

@Alexandermclean
Copy link
Owner

在说项目路由前,可以先看看vue官方给出的vue-router的介绍,我也会列出一些稍微需要注意的点:

1.动态路由参数

用于不同ID的用户都需使用同一个组件渲染

const User = {
  template: '<div>User</div>'
}

const router = new VueRouter({
  routes: [
    // 动态路径参数 以冒号开头
    { path: '/user/:id', component: User }
  ]
})

像 /user/foo 和 /user/bar 都将映射到相同的路由。一个『路径参数』使用冒号 : 标记。当匹配到一个路由时,参数值会被设置到 this.$route.params,可以在每个组件内使用。其中params靠this.$router.push(name or path, params: {id: xx})方法传递,也可以多段设置路由参数:模式:/user/: username/post/:post_id => 匹配路径: /user/evan/post/123 => $route.params: { username: 'evan', post_id: 123 }

2.结合项目

这次采用的是分级的路由定义方式:

import childRouter from '../../router'
const parentPouter1 = {
	path:
	name:
	title:
	component:
	children: childRouter
}
const routerConfig = [ parentPouter1 ]
export default const router = new VueRouter({
	routes: routerConfig
})
// 再在childRouter里面分级定义子路由:
[
	{
		path:
		name:
		title:
		component: () => import('url') //具体页面的路径
	},
	{
		path:
		name:
		title:
		component: () => import('url') //具体页面的路径
	}
]

对于手风琴类的父级展开式的多个子路由的情况:

const parentPouter2 = [
	{
		path:
		name:
		title:
		component:
		children: childRouter1
	},
	{
		path:
		name:
		title:
		component:
		children: childRouter2
	}
]
const routerConfig = [ 
	parentRouter1,
	...parentPouter2  //...分构数组成对象形式的路由
]
export default const router = new VueRouter({
	routes: routerConfig
})

具体关于项目路由结构可以看项目路由

@Alexandermclean Alexandermclean added the Vue Vue label Mar 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Vue Vue
Projects
None yet
Development

No branches or pull requests

1 participant