Import Axios in Mixins Vuejs

Asked

Viewed 461 times

1

Is it possible I import the Axios no Mixins? I have a Rest API and am putting in a Mixin my code of GET, but I use Axios for that. When I try to import the Axios within the Mixins, he returns me:

Uncaught Syntaxerror: Unexpected token import at Mymixins.js:1

Mymixins.js

import axios from 'axios'

let getMixins = {
    created() {
        axios.get(`https://api.myjson.com/bins/17rqo1`)
            .then(response => {
                console.log( response )
            })
            .catch(e => {
                // Errors
            })
    }
}

List.

<template>
   <h1> Test Mixins <h1>
</template>

<script>
    import axios from 'axios'
    export default {
        data: () => ({
            dataItem: [],
            errors: []
        }),
        mixins: [getListagem]
    }
</script>

What am I doing wrong? I’m creating simple variable with . js because I wouldn’t want to use

`import myMixins from ./myMixins`

in my .see

There’s some way to do it?

Thank you!

1 answer

3


The mistake you get about the import I believe it is related to your version of Node or the lack of compiler (webpack).

Anyway what you want to do is possible, but you have to export something in the MyMixins.js. And you really need to require the file whenever you want to add that mixin.

Mymixins.js

import axios from 'axios'

export default const getMixins = {
    created() {
        axios.get(`https://api.myjson.com/bins/17rqo1`)
            .then(response => {
                console.log( response )
            })
            .catch(e => {
                // Errors
            })
    }
}

List.

<template>
   <h1> Test Mixins <h1>
</template>

<script>
    import axios from 'axios'
    import getListagem from './MyMixins'
    export default {
        data: () => ({
            dataItem: [],
            errors: []
        }),
        mixins: [getListagem]
    }
</script>
  • So Sergio, so I know it works, because I would go through my webpack, there is no way I use a Mixins without using import getListagem from './Mymixins' in mine. Vue? Because in the future I will have my . Vue in folder inside folder, then it will be difficult to map by " .. /.. /.. /Mymixins " . Do you understand? Thank you!

  • 1

    Jackson, that’s right, you have to use folder paths ../../. You can use ... getListagem in time of mixins: [getListagem]but to import that object into the file you have to use import or require.

  • Got it! Is there any way I can leave this global Mixin? Where I can access it from any . Go without using import?

  • 1

    @Jackson there’s a way to add mixins globally https://vuejs.org/v2/guide/mixins.html#Global-Mixin but it’s not 100% what you want, as it only adds methods that don’t run each created

  • 1

    Hmm got it. I’m going to study it. Anyway, it helped me a lot! Thank you!! =)

  • @Jackson one thing that occurred to me is that you can have a store in each element and then call the store’s init method.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.