vue获取url中的参数
vue获取url中的参数
1.带参数名
https://www.daima.net?id=123&name=456
获取方式:
方式一(路由获取):
javascript
let data = {
'id': this.$route.params.id,
'name': this.$route.params.name
}
方式二(自定义方法):
javascript
let data = {
'id': this.getParam('id'),
'name': this.getParam('name'))
}
getParam (variable) {
let query = window.location.search.substring(1)
let vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split('=')
if (pair[0] === variable) {
return pair[1]
}
}
return null
}
需要注意的点:
像https://www.daima.net/zhangsan 这种不带参数名的可以直接用路由获取,
如:
javascript
this.$route.params.id