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

    Accessing vue method from external JavaScript

    Help
    3
    6
    696
    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.
    • somascope
      somascope last edited by somascope

      I have some non-Vue/non-Quasar content that I want to be able to call a Vue method from. How can I call a method in my app from other external JavaScript code?

      In this case, I just want to have this other content be able to change the app’s route to go home, like

      this.$router.push('/')
      

      That would be in a Vue method, but I just don’t know how to call that from “outside” the Quasar build.

      Does anybody have ideas on how to do such a thing?

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

        I guess you can import the router instance from the route definition file and just use it

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

          Thanks for the idea, @lucasfernog, but I’m not sure that could be done. The “external JavaScript” that would need to be able to communicate to the Quasar app would be dumb ol’ vanilla JavaScript, with no use of any Vue stuff.

          qyloxe 1 Reply Last reply Reply Quote 0
          • lucasfernog
            lucasfernog last edited by

            I dont understand your use case… maybe the way is to binding context to the function to call, so you can use this.$router directly, but its weird

            1 Reply Last reply Reply Quote 0
            • qyloxe
              qyloxe @somascope last edited by

              @somascope there’s always window events - ie like this:

              ...
                created () {
                  if (window.addEventListener) {
                    window.addEventListener('message', this.listenWindowMessage, false)
                  } else {
                    window.attachEvent('onmessage', this.listenWindowMessage)
                  }
                },
                beforeDestroy () {
                  if (window.addEventListener) {
                    window.removeEventListener('message', this.listenWindowMessage, false)
                  } else {
                    window.detachEvent('onmessage', this.listenWindowMessage)
                  }
                },
                methods: {
                  listenWindowMessage (event) {
                    console.log(event)
                  },
                  ...
              
              1 Reply Last reply Reply Quote 0
              • somascope
                somascope last edited by

                @lucasfernog I’m sorry, I might not have been very clear - thanks for helping though!

                @qyloxe I had begun thinking about events, and then saw your comment. This is exactly what I needed - thank you! Bonus points for including old IE event listeners and also removing the event.

                I’ll dispatch a new event from my non-Vue/Quasar code with:

                window.dispatchEvent(new CustomEvent('myEventNameHere'));
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post