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 the
$routeinstance holds all the information about the current route. The code will console log just the parameter passed in the url. (*Mounted is similar todocument.ready, i.e. it's 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) }, }); <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) }, });