How to pass url parameters to Vuejs

后端 未结 5 2016
感情败类
感情败类 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:46

    either using the $route of the vue:

    const User = {
      template: '
    User {{ $route.params.id }}
    ' } const router = new VueRouter({ routes: [{ path: '/user/:id', component: User }] })

    or you can decouple it by using props

    const User = {
      props: ['id'],
      template: '
    User {{ id }}
    ' } const router = new VueRouter({ routes: [ { path: '/user/:id', component: User, props: true }, // for routes with named views, you have to define the `props` option for each named view: { path: '/user/:id', components: { default: User, sidebar: Sidebar }, props: { default: true, // function mode, more about it below sidebar: route => ({ search: route.query.q }) } } ] })

提交回复
热议问题