Vue2.0路由详解(实例:通过vue-router路由配置实现:类似tab面板的小实例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33867131/article/details/81810035

<head>
# 引入vue
<script type="text/javascript" src='vue.js' ></script>
# 引入vue-router
<script type="text/javascript" src='vue-router.js' ></script>
</head>
<body>

<div id='box'>
	<router-link to='/home'>主页</router-link>
	<router-link to='/news'>新闻页</router-link>
	<router-view></router-view>
</div>

<div>
	//1、创建模板
	<template id='homeid'>
		<div>
			<h1>我是首页home</h1>
		</div>
	</template>
	<template id='newsid'>
		<div>
			<h1>我是新闻news</h1>
			<router-link to='/news/firstnew'>第一篇新闻</router-link>
			<router-view></router-view>
		</div>
	</template>
	<template id='firstnew'>
		<div>
			<h1>这是第一篇新闻:晴天</h1>
		</div>
	</template>
</div>
<script type="text/javascript">
	
	//2、配置路由
	var routes =[
		// '/home' url走Home模板
		{
			'path': '/home', 
			'component': {
				'template': '#homeid'
			}
		},
		// '/news' url走News模板
		{
			'path': '/news', 
			'component': {
				'template': '#newsid'
			}
			// 设置一个下级路由(嵌套路由)
			'children': [
				{
					'path': 'firstnew',
					'component': {
						'template': '#firstnew'
					}
				}
			]
		},

		//你也可以设置默认值
		{
			'path': '/', 
			'redirect':'/home'
		}
	];

	//3、生成路由
	var router = new VueRouter({
		routes
	});
	//4、启动路由(将路由加到vue实例中)
	new Vue({
		router,
		el:'#box'
	})

</script>

</body>

猜你喜欢

转载自blog.csdn.net/qq_33867131/article/details/81810035
今日推荐