How can I get query parameters from a URL in Vue.js?

后端 未结 9 1498
甜味超标
甜味超标 2020-12-12 13:47

How can I fetch query parameters in Vue.js?

E.g. http://somesite.com?test=yay.

Can’t find a way to fetch or do I need to use pure JS or some lib

相关标签:
9条回答
  • 2020-12-12 14:28

    As of this date, the correct way according to the dynamic routing docs is:

    this.$route.params.yourProperty
    

    instead of

    this.$route.query.yourProperty
    
    0 讨论(0)
  • 2020-12-12 14:30

    You can use vue-router.I have an example below:

    url: www.example.com?name=john&lastName=doe

    new Vue({
      el: "#app",
      data: {
        name: '',
        lastName: ''
      },
      beforeRouteEnter(to, from, next) {
          if(Object.keys(to.query).length !== 0) { //if the url has query (?query)
            next(vm => {
             vm.name = to.query.name
             vm.lastName = to.query.lastName
           })
        }
        next()
      }
    })
    

    Note: In beforeRouteEnter function we cannot access the component's properties like: this.propertyName.That's why i have pass the vm to next function.It is the recommented way to access the vue instance.Actually the vm it stands for vue instance

    0 讨论(0)
  • 2020-12-12 14:45

    Try this code

     var vm = new Vue({
         created()
         {
           let urlParams = new URLSearchParams(window.location.search);
               console.log(urlParams.has('yourParam')); // true
               console.log(urlParams.get('yourParam')); // "MyParam"
         },
    
    0 讨论(0)
  • 2020-12-12 14:48

    Without vue-route, split the URL

    var vm = new Vue({
      ....
      created()
      {
        let uri = window.location.href.split('?');
        if (uri.length == 2)
        {
          let vars = uri[1].split('&');
          let getVars = {};
          let tmp = '';
          vars.forEach(function(v){
            tmp = v.split('=');
            if(tmp.length == 2)
            getVars[tmp[0]] = tmp[1];
          });
          console.log(getVars);
          // do 
        }
      },
      updated(){
      },
    

    Another solution https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search:

    var vm = new Vue({
      ....
      created()
      {
        let uri = window.location.search.substring(1); 
        let params = new URLSearchParams(uri);
        console.log(params.get("var_name"));
      },
      updated(){
      },
    
    0 讨论(0)
  • 2020-12-12 14:48

    If your url looks something like this:

    somesite.com/something/123
    

    Where '123' is a parameter named 'id' (url like /something/:id), try with:

    this.$route.params.id
    
    0 讨论(0)
  • 2020-12-12 14:49

    More detailed answer to help the newbies of VueJS:

    • First define your router object, select the mode you seem fit. You can declare your routes inside the routes list.
    • Next you would want your main app to know router exists, so declare it inside the main app declaration .
    • Lastly they $route instance holds all the information about the current route. The code will console log just the parameter passed in the url. (*Mounted is similar to document.ready , .ie its called as soon as the app is ready)

    And the code itself:

    <script src="https://unpkg.com/vue-router"></script>
    var router = new VueRouter({
        mode: 'history',
        routes: []
    });
    var vm =  new Vue({
        router,
        el: '#app',
        mounted: function() {
            q = this.$route.query.q
            console.log(q)
        },
    });
    
    0 讨论(0)
提交回复
热议问题