javascript 在Vue.js中创建Axios / Json服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 在Vue.js中创建Axios / Json服务相关的知识,希望对你有一定的参考价值。

Create e new file src/service/serviceName.js

import axios from 'axios'
    
    const apiClient = axios.create({  
      baseURL: `http://localhost:3000`,
      withCredentials: false, // This is the default
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      }
    })
    
    export default {
      getEvents() {
        return apiClient.get('/events')
      }
      getEvent(id) {
        return apiClient.get('/events/' + id)
      }
    }
    
-------------------- Import in another component 
import ServiceName from '@/services/ServiceName.js'

data() {
  return {
    event: {}
  }
},
created() {
        ServiceName.getEvent(this.id) // <--- Send the prop id to our EventService
          .then(response => {
            this.event = response.data
          })
          .catch(error => {
            console.log('There was an error:', error.response)
          })
      }

以上是关于javascript 在Vue.js中创建Axios / Json服务的主要内容,如果未能解决你的问题,请参考以下文章