How to pass an object as props in Vue 3 using the Vue Router? This can be achieved in Vue 2 following the accepted answer here and its corresponding JSFiddle. In Vue 3 this syntax stringifies pushed objects into "[object Object]" in contrast to Vue 2. I have tried with Vue 3 in the following fiddle.
const Home = { template: '<div>Home</div>',
mounted: function() {
this.$router.push({
name: "about",
params: {
user: "User Name",
objectParam: {a: "a"}
}
})
}}
const About = { template: '<div>About</div>',
props: ['user', 'o', 'objectParam'],
mounted: function(){
console.log( "Passed props:" )
console.log( this.$props )
}}
const routes = [
{ path: '/', component: Home },
{ path: '/about', name:"about", component: About,
props: route => (
console.log("Params defined here are passed as objects."),
console.log("But route params object passed via push has already been stringified."),
console.log(route.params),
{o: {b: "b"},
...route.params
})},
]
const router = VueRouter.createRouter({
history: VueRouter.createWebHashHistory(),
routes,
})
const app = Vue.createApp({})
app.use(router)
app.mount('#app')
Can objects be passed via Vue Router push() in Vue 3?