How to pass url parameters to Vuejs

后端 未结 5 2027
感情败类
感情败类 2021-02-13 05:14

I\'m building an app with laravel and VueJs and I was wondering how to pass a url parameter like the user slug or the user id to vuejs in a proper way to be able to use that pa

5条回答
  •  爱一瞬间的悲伤
    2021-02-13 05:32

    If you are using vue-router and want to capture query parameters and pass them to props, you can do it this way.

    Url being called : http://localhost:8080/#/?prop1=test1&prop2=test2

    MyComponent.vue

    
    
    
    

    router/index.js

    import Vue from 'vue'
    import Router from 'vue-router'
    import MyComponent from '@/components/MyComponent'
    
    Vue.use (Router) 
    Vue.component ('my-component', MyComponent)
    
    export default new Router ({
      routes: [
        {
          path: '/',
          name: 'MyComponent',
          component: MyComponent,
          props: (route) => ({
            prop1: route.query.prop1,
            prop2: route.query.prop2
          })
        }
      ]
    })
    

提交回复
热议问题