简体   繁体   English

未定义Laravel Vue Axios

[英]Laravel Vue Axios is not defined

I built a simple form using the vuetify Framework in my laravel app. 我在laravel应用中使用vuetify框架构建了一个简单表单。 The bootstrap.js already includes the axios Client but I still get the error on submit that axios is not defined: bootstrap.js已经包含axios客户端,但是提交时仍然出现错误,未定义axios:

[Vue warn]: Error in event handler for "click": "ReferenceError: axios is not defined" [Vue警告]:事件处理程序中的“单击”错误:“ ReferenceError:未定义axios”

This is the Default bootstrap.js file which ships with a new laravel Installation, I just removed jquery: 这是新的laravel安装随附的Default bootstrap.js文件,我刚刚删除了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: 这就是我的app.js文件的样子:

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: FormComponent看起来像这样,基本上我正在尝试使用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. 我正在加载axios,所以我不知道为什么会出现该错误。

If you don't want to import it in each component where you use it, you can add it to the Vue prototype: 如果您不想在使用它的每个组件中导入它,可以将其添加到Vue原型中:

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: 在组件中导入axios:

<script>
   import axios from 'axios'
   import { validationMixin } from 'vuelidate'
   ...
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM