:::: 개발 ::::/::: Vue:::

vue.js axios 공통 사용 하도록 변경

nayha 2019. 4. 8. 17:56

axios.js 생성

import axios from "axios"

axios.defaults.baseURL = "https://reqres.in"
axios.defaults.headers.common["Authorization"] = "testcode"
axios.defaults.headers.post["Content-Type"] = "application/json; charset=utf-8"

 

main.js 에 axios import

import axios from "./axios"

new Vue({
  axios,
  render: h => h(App)
}).$mount("#app")

 

각 vue 에서

import axios from "axios"

export default {
  data() {
    return {
      email: "jiyu@korea.com",
      password: null
    }
  },
  computed: {
 
  },
  methods: {
    getlogin() {
      axios
        .get("/api/users?page=2")
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          console.log(err)
        })
        .then(() => {
          // always executed
        })
    },
    postTest() {
      axios
        .post("/api/register", {
          email: "sydney@fife",
          password: "pistol"
        })
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
반응형