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

    Importing Axios

    Help
    4
    5
    3405
    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.
    • M
      MDev last edited by

      Hi,

      I’m not sure how to import Axios into my Quasar project. I’ve installed Axios through npm, but I can’t get it to import into my project. Which file should I be including it in and what should the line that imports it look like?

      Thanks.

      1 Reply Last reply Reply Quote 0
      • D
        dgarcia last edited by

        You should put it in your main.js file:

        import Vue from 'vue'
        import Quasar from 'quasar'
        import router from './router'
        import axios from 'axios'
        import VueAxios from 'vue-axios'
        
        Vue.use(VueAxios, axios)
        
        Vue.use(Quasar) // Install Quasar Framework
        
        Quasar.start(() => {
        	/* eslint-disable no-new */
        	new Vue({
                        el: '#search-app',
        		router,
        		render: h => h(require('./App'))
        	})
        })
        
        1 Reply Last reply Reply Quote 3
        • B
          boriscy last edited by

          if you just use axios, in any module you define

          import axios from 'axios'
          
          export default {
            methods: {
              getItems() {
                 axios.get('/items')
                 .then(res => {
                    console.log(res.data)
                  })
                 .catch( res => {  console.log('error', res) })
              }
            }
          }
          
          1 Reply Last reply Reply Quote 2
          • M
            MDev last edited by

            @dgarcia & @boriscy Thank you very much for the help. It worked perfectly.

            1 Reply Last reply Reply Quote 0
            • L
              Lou Rectoret last edited by Lou Rectoret

              @MDev is better if you set it as a plugin:

              import axios from 'axios'
              
              export default ({ Vue }) => {
                  Vue.prototype.$axios = axios
              }
              

              En then access it as :

                      this.$axios.post('https://localhost/login/').then(res => {
                          console.log(res.data)
                      }).catch(res => { console.log('error', res) })
              
              1 Reply Last reply Reply Quote 0
              • First post
                Last post