Vue中通过URL传递参数

在Vue中,除了使用push 和<router-link :to=''></router-link>进行页面跳转和参数传递之外,还可以使用URL链接进行参数传递,这些参数携带在链接地址后面,比如:/Users/xiaofeiniao/Desktop/Vue/URLParam.html#/home/10001/oo/www.baidu.com,

中间10001 ,oo,www.baidu.com都是在地址中传递的参数,而home是其中的一个子路由,

HTML的测试代码如下:

<html>
<head>
	<script src="https://unpkg.com/vue/dist/vue.js"></script>
	<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<style>

button{

	width: 100px;
	height: 50px;
	background-color: red;
}
</style>

</head>
<body>
	<div id='app'>
		<router-view/>
	</div>
<script>

var helloworld ={
	template:'<div><h1>{{headTitle}}</h1><button @click="gotoPage">跳转到子页面</button><router-view/></div>',
	data:function(){
		return{
			headTitle:'999',
			params:{
				id:10001,
				name:'oo',
				imageUrl:'www.baidu.com'
			}
		}
	},
	methods:{
		gotoPage:function(){
			console.log(this);
			console.log('页面跳转'+JSON.stringify(this.params));
			this.$router.push({
				path:'/home/'+this.params.id+'/'+this.params.name+'/'+this.params.imageUrl
			});
		}
	}
}

var childTemplate ={
	template:'<p>{{$route.params.id}}{{$route.params.name}}{{$route.params.imageUrl}}</p>',
	props:['params'],
	beforeRouteEnter:function(from,to,next){
		console.log('页面进入');
		console.log(from);
		console.log(to);
		next();
	}
}

var router = new VueRouter({
	routes:[
	{
		path:'/',
		component:helloworld,
		children:[
		{
			path:'/home/:id/:name/:imageUrl',
			component:childTemplate,
			name:'childTemplate'
		}
		]
	}

	]
})

new Vue({
	el:'#app',
	data:function() {
	return {
		headTitle:"头部标题"
	}	
	},
	router:router

})

</script>

</body>
</html>

在传递过来的模板页面,可以通过$route.params来获取,比如在上面代码中获取

<p>{{$route.params.id}}{{$route.params.name}}{{$route.params.imageUrl}}</p>

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值