vue hash模式下本地运行和放到服务器上运行,获取参数的方式是不一样的(目前没发现一样的),以下是我经常用的在服务器上和本地获取参数的方法,怕以后忘了,也给有困惑的小伙伴提供一个解决方案。
本地和服务器获取完之后就paramsObj.属性名看查看这个参数就完事了。
本地获取代码:
var paramsObj = {};
let url = decodeURIComponent(window.location.search || window.location.hash)
let strs
if (url.indexOf("?") != -1) {
// hash模式进此判断
if (window.location.hash) {
strs = url.split("?")[1].toString().split("&")
// history模式
} else {
strs = url.substr(1).split("&")
}
// 循环遍历并添加到对象中
for (let i = 0; i < strs.length; i++) {
paramsObj[strs[i].split("=")[0]] = strs[i].split("=")[1]
}
}
服务器获取代码:
let url = decodeURIComponent(window.location.search || window.location.hash)
let strs
if (url.indexOf("?") != -1) {
if (window.location.hash) {
strs = url.split("?")[1].toString().split("&")
} else {
strs = url.substr(1).split("&")
}
for (let i = 0; i < strs.length; i++) {
var num = strs[i].indexOf("=");
var name = strs[i].substr(num + 1);
paramsObj[strs[i].split("=")[0]] = name
}
}
因为本人对前端这些不是很熟悉,有好方法的小伙伴欢迎留言指正评论。