1. 程式人生 > 實用技巧 >vue-resource的使用、axios的使用

vue-resource的使用、axios的使用

1、vue-resource的使用

  1、直接在頁面中,通過script標籤,引入vue-resource的指令碼檔案;

  2、注意:引用的先後順序是-先引用Vue的指令碼檔案,再引用vue-resource的指令碼檔案。

  3、vue-resource get請求案例

<div id='app'>
        <ul>
            <li v-for="item in list">
                <img :src="item.coverImgUrl" alt="">
                <p>{{item.name}}</p>
            </li>
        </ul>
    </div>
    <script>
    const
vm = new Vue({ el: '#app', data: { list:[], }, created(){ this.$http.get('https://showme.myhope365.com/api/shop/shopGoods/open/banner/list',{ header:{ 'X-Requested-With':'XMLHttpRequest' } }).then(res
=>{ this.list=res.body.rows; }).catch(err=>{ console.error(err); }) } }) </script>

4、vue-resource post請求案例

 <div id='app'>
        <ul>
            <li v-for="item in list">
                <img :src="
item.coverImgUrl" alt=""> <p>{{item.name}}</p> </li> </ul> </div> <script> const vm = new Vue({ el: '#app', data: { list:[], }, created(){ this.$http.post('https://showme.myhope365.com/api/shop/shopGoods/open/list',{ pageNum:1, pageSize:5, },{ emulateJSON:true }).then(res => { console.log(res); this.list = res.body.rows; }).catch(err => { console.error(err); }) } }) </script>

2、axios的使用

  1、axios get請求

    axios.get(地址,配置).then(res=>{ })

  <div id='app'>
        <ul>
            <li v-for="item in list">
                <img :src="item.coverImgUrl" alt="">
                <p>{{item.name}}</p>
            </li>
        </ul>
    </div>
    <script>
    const vm = new Vue({
        el: '#app',
        data: {
            list:[],
        },
        created(){
            axios.get('https://showme.myhope365.com/api/shop/shopGoods/open/banner/list',{
                headers:{
                    'X-Requested-With': 'XMLHttpRequest'
                }
            }).then(res => {
                console.log(res);
                this.list = res.data.rows;
            }).catch(err => {
                console.error(err);
            })
        }
    })
    </script>

2、axios post請求   

// json {}
// application/x-www-form-urlencoded   URLSearchParams()
// multipart/form-data   FormData()  
const formdata = new FormData()
formdata.append("引數名",引數值)                          
axios.get(地址,請求體,配置).then(res=>{})

 <div id='app'>
        <ul>
            <li v-for="item in list">
                <img :src="item.coverImgUrl" alt="">
                <p>{{item.name}}</p>
            </li>
        </ul>
    </div>
    <script>
    const vm = new Vue({
        el: '#app',
        data: {
            list:[],
        },
        created(){
            //  axios.post('https://showme.myhope365.com/api/shop/shopGoods/open/list', {
            //     pageNum: 1,
            //     pageSize: 5,
            // }).then(res => {
            //     console.log(res);
            //     this.list = res.body.rows;
            // }).catch(err => {
            //     console.error(err);
            // })
            let params=new URLSearchParams();
            params.append("pageNum", 1);
            params.append("pageSize", 5);
            axios.post('https://showme.myhope365.com/api/shop/shopGoods/open/list',params).then(res => {
                console.log(res);
                this.list = res.data.rows;
            }).catch(err => {
                console.error(err);
            })
        }
    })
    </script>

3、axios檔案上傳

<div id='app'>
        <input type="file" @change="selectFile">
        <button @click="uploadFile">上傳檔案</button>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                file: null,
            },
            methods: {
                selectFile(e) {
                    this.file = e.target.files[0];
                },
                uploadFile() {
                    let formdate = new FormData();
                    formdate.append("file", this.file);
                    formdate.append("fileUseForEnum", "DEFAULT");
                    axios.post('http://59.111.92.205:13010/api/nos/upload/image', formdate).then(res => {
                        console.log(res);
                    })
                }
            }
        })
    </script>