vue不经过路由直接获取地址栏参数的方法

vue中是没法经过location.search()来获取url问号以后的内容的,因此就须要把获取参数的脚本代码注册成全局方法vue

一、components目录同级新建文件夹api用来保存js脚本,而后新建utils.js编写js方法api

    例如:url

    //获取地址栏参数code

    export function getUrlKey(name) {component

    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;get

    }io

二、在须要的地方引入方法就能够使用function

    import { getUrlKey } from "../api/utils";import

    let UrlKey = getUrlKey("code");方法

相关文章
相关标签/搜索