Passing props with programmatic navigation Vue.js

前端 未结 3 777
失恋的感觉
失恋的感觉 2020-12-05 01:59

I have a Vue component that has a prop named \'title\' e.g:



        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 02:49

    The vue-router docs clearly state params only work with name and not path.

    // set  props: true in your route definition
    const userId = 123
    router.push({ name: 'user', params: { userId }}) // -> /user/123
    // This will NOT work
    router.push({ path: '/user', params: { userId }}) // -> /user
    

    If you use path, pass the params in the path itself or use query as demonstrated below:

    router.push({ path: `/user/${userId}` }) // -> /user/123
    
    // with query, resulting in /register?plan=private
    router.push({ path: 'register', query: { plan: 'private' }})
    

提交回复
热议问题