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

后端 未结 9 1515
甜味超标
甜味超标 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: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:

    
    var router = new VueRouter({
        mode: 'history',
        routes: []
    });
    var vm =  new Vue({
        router,
        el: '#app',
        mounted: function() {
            q = this.$route.query.q
            console.log(q)
        },
    });
    

提交回复
热议问题