SoFunction
Updated on 2025-04-07

Example of method of vue not directly obtaining parameters in url through routing

Preface

As we all know, the URL parameters are set in vue using routing, but this method must be accompanied by parameters in the path, and this parameter needs to be set in advance in vue's route.

Relatively speaking, in some cases, splicing directly behind the url?mid=100The method of passing parameters is more flexible. You don't need to set up a route, you just need to splice the parameters after the url. However, this method requires you to obtain and extract the parameters in the url through Javascript. It is not feasible to obtain directly on the page through traditional methods, because it is impossible to pass in vue.()To get the content after the url question mark.

Of course, there is also a solution to this problem, which is to register the script code that obtains parameters as a global method. I won’t say much below, let’s take a look at the detailed method examples.

Sample code

Step 1: Create the file and save it to the project root directory

 export default{
  getUrlKey:function(name){
   return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec()||[,""])[1].replace(/\+/g,'%20'))||null;
  }
 }

Step 2: Register the global method in the main js method ()

 import utils   from './utils'    //Get url parameters 
 .$utils=utils   //Register global method

Step 3: Reference method in vue file

 let channel=this.$("channel")

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.