最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Laravel Vue Axios is not defined - Stack Overflow

programmeradmin0浏览0评论

I built a simple form using the vuetify Framework in my laravel app. The bootstrap.js already includes the axios Client but I still get the error on submit that axios is not defined:

[Vue warn]: Error in event handler for "click": "ReferenceError: axios is not defined"

This is the Default bootstrap.js file which ships with a new laravel Installation, I just removed jquery:

window._ = require('lodash');
window.Popper = require('popper.js').default;

window.axios = require('axios');

window.axios.defaults.headersmon['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headersmon['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: ');
}

And this is how my app.js file Looks like:

import bootstrap from 'bootstrap';

import 'material-design-icons-iconfont/dist/material-design-icons.css' // Ensure you are using css-loader

import Vue from 'vue';
import Vuetify from 'vuetify';
import 'vuetify/dist/vuetify.min.css';

Vue.use(Vuetify);

Vueponent('form-component', require('./components/FormComponent.vue'));

const app = new Vue({
    el: '#app',

    data: {
        loading: true
    },

});

The FormComponent Looks like this, basically I am trying to post the data using axios:

<template>
    <v-container grid-list-xl>
        <v-flex>
            <v-text-field
                v-model="name"
                :error-messages="nameErrors"
                :counter="10"
                label="Name"
                required
                @input="$v.name.$touch()"
                @blur="$v.name.$touch()"
            ></v-text-field>
        </v-flex>
        <v-flex>
            <v-btn @click="submit">submit</v-btn>
            <v-btn @click="clear">clear</v-btn>        
        </v-flex>
    </v-container>
</template>

<script>
import { validationMixin } from 'vuelidate'
import { required, maxLength, email } from 'vuelidate/lib/validators'

export default {
    mixins: [validationMixin],

    validations: {
        name: { required, maxLength: maxLength(20) }
    },

    data() {
        return {
            reactive: false,
            name: '',
        }
    },

    computed: {
        nameErrors () {
            const errors = [];
            if (!this.$v.name.$dirty) {
                return errors;
            } 
            !this.$v.name.maxLength && errors.push('Name must be at most 10 characters long');
            !this.$v.name.required && errors.push('Name is required.');
            return errors;
        },
    },

    methods: {
        submit () {
            this.$v.$touch(); 
            axios.post('event/store', {
                'name': this.name,
            })
            .then((response) => {
                console.log("success");
            })
            .catch((error) => {
                console.log("error");
            })
        },
        clear () {
            this.$v.$reset();
            this.name = '';
        }
    }

}
</script>

I am loading axios so I do not know why that error appears.

I built a simple form using the vuetify Framework in my laravel app. The bootstrap.js already includes the axios Client but I still get the error on submit that axios is not defined:

[Vue warn]: Error in event handler for "click": "ReferenceError: axios is not defined"

This is the Default bootstrap.js file which ships with a new laravel Installation, I just removed jquery:

window._ = require('lodash');
window.Popper = require('popper.js').default;

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

And this is how my app.js file Looks like:

import bootstrap from 'bootstrap';

import 'material-design-icons-iconfont/dist/material-design-icons.css' // Ensure you are using css-loader

import Vue from 'vue';
import Vuetify from 'vuetify';
import 'vuetify/dist/vuetify.min.css';

Vue.use(Vuetify);

Vue.component('form-component', require('./components/FormComponent.vue'));

const app = new Vue({
    el: '#app',

    data: {
        loading: true
    },

});

The FormComponent Looks like this, basically I am trying to post the data using axios:

<template>
    <v-container grid-list-xl>
        <v-flex>
            <v-text-field
                v-model="name"
                :error-messages="nameErrors"
                :counter="10"
                label="Name"
                required
                @input="$v.name.$touch()"
                @blur="$v.name.$touch()"
            ></v-text-field>
        </v-flex>
        <v-flex>
            <v-btn @click="submit">submit</v-btn>
            <v-btn @click="clear">clear</v-btn>        
        </v-flex>
    </v-container>
</template>

<script>
import { validationMixin } from 'vuelidate'
import { required, maxLength, email } from 'vuelidate/lib/validators'

export default {
    mixins: [validationMixin],

    validations: {
        name: { required, maxLength: maxLength(20) }
    },

    data() {
        return {
            reactive: false,
            name: '',
        }
    },

    computed: {
        nameErrors () {
            const errors = [];
            if (!this.$v.name.$dirty) {
                return errors;
            } 
            !this.$v.name.maxLength && errors.push('Name must be at most 10 characters long');
            !this.$v.name.required && errors.push('Name is required.');
            return errors;
        },
    },

    methods: {
        submit () {
            this.$v.$touch(); 
            axios.post('event/store', {
                'name': this.name,
            })
            .then((response) => {
                console.log("success");
            })
            .catch((error) => {
                console.log("error");
            })
        },
        clear () {
            this.$v.$reset();
            this.name = '';
        }
    }

}
</script>

I am loading axios so I do not know why that error appears.

Share Improve this question edited Jul 25, 2018 at 3:37 Ts8060 1,0708 silver badges19 bronze badges asked Jul 24, 2018 at 20:22 tigereltigerel 3372 gold badges8 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

If you don't want to import it in each component where you use it, you can add it to the Vue prototype:

window.axios = require('axios');

//... configure axios...

Vue.prototype.$http = window.axios;

Then in any component you can use

this.$http.post('event/store', {
    'name': this.name,
})

Import axios in the component:

<script>
   import axios from 'axios'
   import { validationMixin } from 'vuelidate'
   ...
</script>
发布评论

评论列表(0)

  1. 暂无评论