question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use route() in setup() of vue 3 component?

See original GitHub issue

Ziggy 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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
sprklinginfocommented, Aug 27, 2021

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:

vueApp.mixin({ methods: { appRoute: route } });

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.

0reactions
fahmiegertoncommented, Jan 9, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found