javascript - Laravel Vue Axios 未定义

标签 javascript laravel vue.js axios

我在我的 laravel 应用程序中使用 vuetify 框架构建了一个简单的表单。 bootstrap.js 已经包含了 axios 客户端,但我仍然在提交时收到错误,即 axios 未定义:

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

这是新的 laravel 安装附带的默认 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');
}

这就是我的 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
    },

});

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>

我正在加载 axios,所以我不知道为什么会出现该错误。

最佳答案

如果你不想在每个使用它的组件中引入它,你可以将它添加到Vue原型(prototype)中:

window.axios = require('axios');

//... configure axios...

Vue.prototype.$http = window.axios;

然后在任何你可以使用的组件中

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

关于javascript - Laravel Vue Axios 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51506947/

相关文章:

javascript - 将 angularjs 转换为纯 js 或如何将单个服务器用于 angular JS 和 Flask

mysql - 如何获得所有最大列值的总和

php - Laravel API : RegisterController set a default value on creating an User not working

javascript - 使用 Vee-Validate 和 vue js 2 在提交时验证子输入组件

javascript - VueJS 输入与嵌套数据的绑定(bind)

javascript - 如何在 vue 组件中本地使用 VeeValidate 或任何第三方包

javascript - 如何使用 Javascript 或 jQuery 更改 select 元素的每个选项的内容

javascript - jQuery,如何设置多个输入需要通知输入

javascript - jQuery:用字符串替换多个正则表达式匹配项?

mysql - laravel php artisan 迁移问题