Home Web Front-end JS Tutorial On the organization method of Vue project API related code

On the organization method of Vue project API related code

Mar 31, 2018 am 10:43 AM
api Way organize

This article mainly shares the organization method of the vue project api related code. Friends in need can refer to it

On the organization method of the vue project api related code

Read After downloading the code of colleagues in the project team, I found that different projects have different organizational versions

Version 1:

├─apis
│      a.api.js
│      b.api.js
│      b.api.js
│      d.api.js
Copy after login

Every api file has such code

// d.api.js
import axios from '@/utils/http'

export function editUser (Param) {
    return axios.post('url1', {
        ...Param
    })
}
export function deleteUser (Param) {
    return axios.post('url2', {
        ...Param
    })
}
// 调用方式如下
import {editUser} from '@/apis/d.api.js'
Copy after login

Disadvantages of this method:

  1. When you add an excuse, add a new method

  2. Anywhere that needs to call an excuse Need to be imported

  3. Only the url and function name in the api file are different, the others are the same and should be packaged together

  4. You need one to view all interfaces It’s troublesome to look at a function

Version 2:
Just don’t unify the API together, mount axios to the vue object only when needed Write

this.$axios.post(url,params).then()
Copy after login

in the place. Disadvantages of this method:

  1. If you modify the url path, you need to globally search and replace many places to change

  2. Unable to view all interfaces, inconvenient for global control

##Version 3:

// apis/index.js
// 把所有api的url统一在一起并挂在到vue对象上
// 所有接口都在一个文件里会比较大
// 可以按功能模块分组编写
let ENV = {
    name1: 'url1', 
    // 用户相关接口
    name2: 'url2', 
    // 积分相关接口
    name3: 'url3',
    // 产品相关接口
    name4: 'url4', 
}
export default ENV
Copy after login
// src/main.js
import api from '@/apis/index.js'
Vue.prototype.$api = api
Copy after login
//需要调用接口的js文件
this.$axios.post(this.$api.name1,params).then()
Copy after login
Disadvantages:

  1. I didn’t expect it for the time being

Advantages:

  1. When changing the url, you only need to change one place

  2. You can view all interfaces in one place

Related recommendations:

apicloud implements AJAX requests (with code)

Canvas drawing api usage details

The above is the detailed content of On the organization method of Vue project API related code. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to crawl and process data by calling API interface in PHP project? How to crawl and process data by calling API interface in PHP project? Sep 05, 2023 am 08:41 AM

How to crawl and process data by calling API interface in PHP project?

React API Call Guide: How to interact and transfer data with the backend API React API Call Guide: How to interact and transfer data with the backend API Sep 26, 2023 am 10:19 AM

React API Call Guide: How to interact and transfer data with the backend API

Oracle API Usage Guide: Exploring Data Interface Technology Oracle API Usage Guide: Exploring Data Interface Technology Mar 07, 2024 am 11:12 AM

Oracle API Usage Guide: Exploring Data Interface Technology

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

Oracle API integration strategy analysis: achieving seamless communication between systems

Save API data to CSV format using Python Save API data to CSV format using Python Aug 31, 2023 pm 09:09 PM

Save API data to CSV format using Python

How to deal with Laravel API error problems How to deal with Laravel API error problems Mar 06, 2024 pm 05:18 PM

How to deal with Laravel API error problems

PHP API Interface: Getting Started Guide PHP API Interface: Getting Started Guide Aug 25, 2023 am 11:45 AM

PHP API Interface: Getting Started Guide

PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences Aug 27, 2023 pm 12:22 PM

PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences

See all articles