如何使用npm包:Laravel 5.5中的axios?

时间:2017-11-07 14:34:46

标签: laravel vue.js axios

我想在Laravel中使用Axios进行AJAX请求。我做了npm install axios --save,但我不知道如何使用它。请帮助我如何在我的laravel项目中使用axios。

这是我的ajax-land.blade.php:

<div id="app">
    <ul>
        <li v-for = "skill in skills" v-text = "skill"></li>
    </ul>
</div>
<script src = "https://vuejs.org/js/vue.js"></script>
<script src = "/js/axisa.js"></script>

这是我的axisa.js:

 new Vue({
    el: '#app',
    data: {
        skills: []
    },
    mounted(){
        axios({
            method: 'get',
            url: '/links'
        })
        .then(response => this.skills = response.data)
    }
});

以下是我的路线:

Route::get('/', function () {
    return view('ajax-land');
});

Route::get('/links', function(){
    return ['Google', 'Microsoft', 'Facebook', 'Twitter', 'LinkedIn'];
});

这是bootstrap.js:

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap-sass');
} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

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

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

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');
}

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from 'laravel-echo'

// window.Pusher = require('pusher-js');

// window.Echo = new Echo({
//     broadcaster: 'pusher',
//     key: 'your-pusher-key'
// });

这是app.js:

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

有一些教程可以通过import axios from 'axios';在bootstrap.js中导入axios,并且在axisa.js中也有window.axios = axios,但由于某种原因它不起作用。请帮助我如何在Laravel中使用axios。

2 个答案:

答案 0 :(得分:2)

Axios是一个基于简单承诺的HTTP客户端。在Laravel中,它默认位于窗口对象上,所以只需使用如下:

获取

axios.get('/some-path').then(res => {
    console.log(res.data)
})

发表

let data = {
    some: "thing"
}
axios.post('/some-path', data).then(res => {
    console.log(res.data)
})

Documentation和RTFM一样非常好。

编辑1

如果在axios对象上未定义,请使用

import axios from 'axios'

位于JS文件的顶部,如果您完全缺少Axios,npm install axios --save

答案 1 :(得分:0)

在您的资源/assetss/js/app.js中

简单添加: var axios = require('axios');

开始 从'axios'导入axios -似乎简直是