vue获取url中的参数
小开开 2020-09-14 11:33:34 2020-09-14 343 0
vue获取url中的参数
1.带参数名
https://www.daima.net?id=123&name=456获取方式:方式一(路由获取):
let data = { 'id': this.$route.params.id, 'name': this.$route.params.name}
方式二(自定义方法):
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 这种不带参数名的可以直接用路由获取,如:this.$route.params.id