vue mixins处理统一封装的axios

  • 目的是为了多个组件中使用同一个axios方便,不用每个组件中都import一遍balabla

1.建个文件,我的api下是封装好的axios,commonvuejs作为mixins
vue mixins处理统一封装的axios_第1张图片
2.在commonvuejs

<script>
import { weiboPicUrlAPI } from "../../api/WsendQuestion";//引入封装axiosApi
export default {
  name: "mixins-test-main",
  props: {},
  methods: {
    async weiboPicUrlAPI(data) {
      let res = await weiboPicUrlAPI(data);
      return res.data;
    },
  },
};
</script>

3.在页面使用

<script>
import commonvuejs from "../assets/js/commonvuejs.vue";//引入mixins文件
export default {
  mixins: [commonvuejs],
  mounted() {
    this.weiboPicUrlAPI();//直接使用
  },
</script>

你可能感兴趣的:(vue.js,javascript,前端)