vue hash模式下怎么获取参数

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
                }
            }

因为本人对前端这些不是很熟悉,有好方法的小伙伴欢迎留言指正评论。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值