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

    Global Directives & components

    Help
    2
    6
    2544
    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.
    • S
      synbits last edited by

      Hello,

      When I import for example Toast and QBtn in my main.js

      # Example in main.js
      import Quasar, {
        Toast,
        QBtn
      } from 'quasar'
      
      ...
      
      Vue.use(Quasar, {
        components: {
          QBtn
        },
        directives: 
          Toast
        }
      })
      

      I still need to import Toast in my *.vue files but not QBtn?

      # Example in edit.vue
      import {
          Toast
      } from 'quasar'
      
      

      Is it possible to import Toast globally and not in each file? Or what am I doing wrong (read not seeing)?

      Thanks!

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

        Toast is not a directive. directives are things like

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

          Toast is not a directive. directives are things like v-ripple that go as properties on dom items. Like this:

          <div v-ripple></div>
          

          Toast is an object with methods. You have to import it in each component that uses it. Or you could do this (always make sure the you are no overwriting anything when doing this):

          # Example in main.js
          import Quasar, {
            Toast
          } from 'quasar'
          
          ...
          // before new Vue()
          Vue.prototype.$toast = Toast
          
          
          

          With this you can access this.$toast the same way you access this.$router anywhere in your app

          1 Reply Last reply Reply Quote 1
          • S
            synbits last edited by

            Thanks, this is the answer I needed!

            1 Reply Last reply Reply Quote 0
            • S
              synbits last edited by

              Can I do the same with a normal directive like GoBack? Only add it to main.js somehow?

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

                Make component /directive globally available:

                // Example in main.js
                import Quasar, {
                  Ripple,
                  QBtn
                } from 'quasar'
                
                ...
                
                Vue.use(Quasar, {
                  components: {
                    QBtn
                  },
                  directives: 
                    Ripple
                  }
                })
                

                Note this does not take advantage of tree shaking. To maximize loading speed, best load components in each view.
                You could add anything to the Vue prototype, make sure not to overwrite anything.
                Again, best not overload it with something you don’t use all the time. I usually add axios as $http

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