How to use route() in setup() of vue 3 component?
See original GitHub issueZiggy version
v1.3.5
Laravel version
v8.54.0
Description
Working on a project with Laravel/Inertia stack (from breeze starter kit). Here is a part of code in app.js
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => require(`./Pages/${name}.vue`),
setup({ el, app, props, plugin }) {
return createApp({ render: () => h(app, props) })
.use(plugin)
.use(ElementPlus, { locale })
.mixin({ methods: route }) // avoid namespace conflict with Element Plus NaveMenu component
.mount(el);
},
});
I am trying to use composition API on my components. I got ‘route() undefined’ error if I try using ‘ctx.route()’. So my question is how I can use route() helper in setup() to access route params? Thanks!
Ziggy call and context
//
Ziggy configuration
//
Route definition
//
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Vue Router and the Composition API
The introduction of setup and Vue's Composition API, open up new possibilities but to be able to get the full potential out of...
Read more >Routing in Vue3: Navigating the Options - CODE Magazine
To start out, you'll need to create an array of objects that describe each route. A route typically has a name, a path,...
Read more >Vue.js 3 Composition API Routing Tutorial - KoderHQ
Learn how the router works with the Composition API. We cover how to create a reference to the router for navigation and access...
Read more >How to use router in vue composition api? - Stack Overflow
You can just do this: import { useRoute } from 'vue-router'; export default { setup() { const route = useRoute(); // Now you...
Read more >Vue Router: A Tutorial for Vue 3 - Vue Mastery
If you're looking to learn the new Vue Router that uses Vue 3, you're in ... history: createWebHistory(), routes, }); export default router;....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Million thanks @bakerkretzmar. One thing I didn’t mention is that I had to alias the route() to appRoute() in the app.js since I also use Element Plus UI, which seems to conflict with its navMenu component:
so in template, appRoute(‘register’) etc. works. but in setup() it gave out ‘appRoute is not defined’ error. so I tried with options api, e.g. in data() to access this.appRoute().params which has no errors. After reading your new reply, I tried route() in setup() it works!!
when I tried with the second solution before, I didn’t remove .mixin({ methods: { route } }) from the app.js, I guess that is why it didn’t work.
I’ve got
route is not a function
when using with axios, but make it as a seperate function inside the setup and now it works!