文件路徑:/src/common/api/portal/news.js
~~~
import axios from "axios";
import Setting from "@/setting";
const API_BASE_URL = Setting.apiBaseUrl;
// const API_BASE_URL = "http://127.0.0.1:8360";
export default {
/**
* 獲取新聞動態的分頁數據
* @param {Object} map 查詢條件對象
* @return {Promise<Object>} 新聞動態的分頁數據
*/
async getList(map = { page: 1, pageSize: 10, keywords: "" }) {
// 上面的請求也可以這樣做
return axios
.get(`${API_BASE_URL}/api/portal/news/getList`, {
params: map,
})
.then(function(response) {
// console.log(response.data);
//只返回與業務數據有關的data域,其他的都是與網絡請求有關
return new Promise((resolve, reject) => {
resolve(response.data);
});
})
.catch(function(error) {
console.log(error);
});
},
async getDetail(id) {
// 上面的請求也可以這樣做
return axios
.get(`${API_BASE_URL}/api/portal/news/getDetail`, {
params: { id },
})
.then(function(response) {
return new Promise((resolve, reject) => {
resolve(response.data);
});
})
.catch(function(error) {
console.log(error);
});
},
};
~~~
文件路徑:/src/setting.js
~~~
const env = process.env.NODE_ENV;
const Setting = {
apiBaseUrl: "http://127.0.0.1:8360",
};
export default Setting;
~~~
修改接口的入口文件`/src/common/api/portal/index.js`,換成遠程的請求
修改
~~~
import news from "./mock/news.js";
~~~
改為
~~~
import news from "./news.js";
~~~
文件路徑:/src/common/api/portal/index.js
~~~
import news from "./news.js";
/**
* @example
* import {news} from "@/common/api/portal";
* news.getList(this.map).then((res) => {
* if (res.errno == 0) {
* const tableData = res.data;
* // ...
* });
* } else {
* console.log(res.errmsg)
* }
* });
*/
export { news };
~~~
## 代碼解釋
- 文檔說明
- 服務端開發指南
- 客戶端開發指南
- 請求攔截器
- API接口實例分析
- 頁面文件
- NPM包管理
- 創建NPM包項目
- 課程設計
- 概述
- 內容管理系統項目
- 配置開發環境
- 設計靜態原型
- 快速構建項目
- 構建CMS系統前端界面
- 門戶模塊
- 新聞列表
- API接口規范
- 生成模擬數據
- 顯示新聞列表
- NavigatorPath組件
- ChannelHeader組件
- v-line-clamp指令
- formatDate過濾器
- 新聞詳情頁
- 修改頂部導航菜單
- 實現訪問遠程API
- 擴展功能
- 組件開發
- 服務端項目
- 編寫服務模塊
- 項目配置
- 數據庫
- 創建數據庫腳本
- 配置數據庫
- 創建模擬數據
- 新聞模塊控制器
- 添加邏輯驗證層
- 實現接口
- 書棧模塊
- QA