No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Access $router outside vue

    Framework
    router
    6
    10
    5509
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      reath last edited by

      How can I access $router in a .js file?

      1 Reply Last reply Reply Quote 0
      • M
        Max last edited by Max

        i would try to import the router where you need it:

        import router from '/router/index';  //replace with your correct path
        
        1 Reply Last reply Reply Quote 0
        • R
          reath last edited by

          Tried that, but the line
          router().replace({name: 'logout'}) only changes the url, without anything happening on the UI. Things work after I click F5

          1 Reply Last reply Reply Quote 0
          • Allan-EN-GB
            Allan-EN-GB Admin last edited by

            router().push({name: 'logout'})

            1 Reply Last reply Reply Quote 1
            • R
              reath last edited by

              Both router().replace({name: 'logout'}) and router().replace({name: 'logout'}) only change the URL, but nothing more happens. The page isn’t changed, no errors are shown in the console. I have to click F5 and then things work (because the URL is changed to the right one)

              1 Reply Last reply Reply Quote 0
              • rstoenescu
                rstoenescu Admin last edited by

                Use boot files where you have access to the instance of the Router (as param to the default export method).

                If you import and call router() you are essentially creating ANOTHER instance of the router, so nothing can actually happen to your app since your app is connected only to the initial Router.

                1 Reply Last reply Reply Quote 1
                • R
                  reath last edited by reath

                  Thank you @rstoenescu for the guidelines. I’m trying to use the router in a vuex action inside of a module. How would I transfer that part of the redirect logic in a boot file?

                  1 Reply Last reply Reply Quote 0
                  • metalsadman
                    metalsadman last edited by metalsadman

                    @reath follow what @rstoenescu suggested, then import that boot file in your vuex.
                    edit. something like

                    // boot/router.js
                    let routerInstance = void 0
                    export default async ({ router }) => {
                      // something to do
                      routerInstance = router
                    }
                    export { routerInstance }
                    
                    // store/somemodule/action.js
                    import { routerInstance } from 'boot/router'
                    export const someAction = (...) => {
                      ... 
                      routerInstance.push('/some-route')
                    }
                    
                    1 Reply Last reply Reply Quote 3
                    • rstoenescu
                      rstoenescu Admin last edited by

                      If you’re using this in a Vuex store file, then it will suffice to access “this.$router”. Just make sure you don’t define your actions with ES6 arrow syntax (because “this” will mean something else as an effect).

                      export const someAction(...) {
                         //...
                        this.$router......
                      }
                      
                      1 Reply Last reply Reply Quote 3
                      • R
                        realtebo last edited by

                        What are your tring to accomlish?

                        In a scenario, I need to command the routing using electron menu. In this case I use electron function to fire an event and inside vue I can access a ‘bridge’ to listen for electron-initiated events. So user click on a menu and vue router change the page

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post