首页 > 技术文章 > VUE - 调用ODATA接口

1285026182YUAN 2021-05-20 18:08 原文

VUE - 调用ODATA接口

 

o.js源码:https://github.com/janhommes/o.js ,用法可以在里面找

1. 安装插件

cnpm install odata --save-dev

yarn add odata --save-dev

 

 

2. 新建一个接口调用的js文件

 

引入 odata

import {  o } from 'odata';

定义 域名地址

const baseURL = `http://192.168.1.100:8088/odata/`;

 

 Get 方法 

export async function api_getOpeList(parameter) {
  let response = await o(`${baseURL}`).get('方法名').query(parameter);
  return response;
}

parameter样例参数如下
 let parameter = {
            $filter: `Id eq 3`,
            $expand: 'nodes($select=Id,Name,ParentId)',//连接nodes
            $select: `Id,nodes`,
          };
 

 

 Post 方法 

export async function api_postOpeList(parameter) {
  let response = await o(`${baseURL}`).post('方法名',parameter).query();
  return response;
}

parameter样例参数如下
let
parameter={
Id:3,
Name:'张三',
Age:45
}

 

Patch 方法 

//参数具体跟据后端方法
export async function api_patchData(key, parameter) { let response = await o(`${baseURL}`).patch(`方法名(主键参数值)`, parameter).query(); return response; }


parameter样例参数如下
let parameter={
Id:3,
Name:'张三',
Age:45
}
 

 

 Batch 方法(批量操作)

export async function api_postBatchData() {
  let response = await o(`https://localhost:5001/odata/`)
    .get('books')
    .get('dtoo')
    .post('books', { icc: 11112 , name: 'zhangsan' } )
    .batch();
  return response;
}

 

 POSTMAN

https://localhost:5001/odata/$batch

{
"requests": [ { "id": "aa", "method": "POST", "headers": { "content-type": "application/json; odata.metadata=minimal; odata.streaming=true; charset=utf-8" }, "body": { "icc": 122, "name": "zhangsan" }, "url": "https://localhost:5001/odata/Books" }, { "id": "sldfk", "method": "GET", "url": "https://localhost:5001/odata/dtoo" }, { "id": "sdfww", "method": "GET", "url": "https://localhost:5001/odata/Books" } ] }

 

 

 

 

 

  

 

引用:https://www.npmjs.com/package/odata

引用:https://github.com/janhommes/o.js

推荐阅读