vue接通后端api以及部署到服務(wù)器操作
1.打開(kāi)項(xiàng)目工程,找到config文件夾下index.js,進(jìn)行以下修改
dev: { // Paths assetsSubDirectory: ’static’, assetsPublicPath: ’/’, proxyTable: { ’/api’:{ target: ’http://www.baidu.com’,//后端api地址 changeOrigin: true, pathRewrite:{ ’^api’: ’’ } } },
2.然后打開(kāi)src下App.vue文件配置默認(rèn)前綴
export default { name: ’App’, created: function () { this.$http.defaults.baseURL = ’https://www.baidu.com/api’ //后端api默認(rèn)前綴,每個(gè)請(qǐng)求都加上這個(gè)前綴訪(fǎng)問(wèn)后臺(tái)api }}
3.打開(kāi)項(xiàng)目工程,找到config文件夾下prod.env.js,進(jìn)行以下修改
’use strict’module.exports = { NODE_ENV: ’'production'’, API_HOST: ’'http://www.baidu.com'’//后端api地址}
4.找到config文件夾下dev.env.js,進(jìn)行以下修改
’use strict’const merge = require(’webpack-merge’)const prodEnv = require(’./prod.env’) module.exports = merge(prodEnv, { NODE_ENV: ’'development'’, API_HOST: ’'http://localhost:8080'’//這里是本地的訪(fǎng)問(wèn)ip配置})
5.然后 npm run build 對(duì)項(xiàng)目文件進(jìn)行打包,完成后在項(xiàng)目根目錄下生成dist文件夾,把dist文件夾上傳到服務(wù)器即可
補(bǔ)充知識(shí):Vue全局變量配置(多用于調(diào)用后端API)
我們?cè)谑褂肰ue時(shí),通常需要調(diào)用后端API進(jìn)行一系列的操作。
下面分享一個(gè)我的配置方案。
1.變量分類(lèi)配置
新建文件,加入配置內(nèi)容如下:
export const apiAddress = { install(Vue){ Vue.prototype.$javaAddress = ’11’; }};export const config = { install(Vue){ Vue.prototype.$config = ’1’; }};export default { apiAddress, config };
在main.js中引入配置
import { apiAddress, config } from ’./config/address’;Vue.use(apiAddress);Vue.use(config);
2.目前我在用的
export default { install(Vue){ Vue.prototype.$javaAddress = ’111’; }};import address from ’./config/address’;Vue.use(address);
以上這篇vue接通后端api以及部署到服務(wù)器操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法2. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……3. xml中的空格之完全解說(shuō)4. php bugs代碼審計(jì)基礎(chǔ)詳解5. WMLScript的語(yǔ)法基礎(chǔ)6. ASP使用MySQL數(shù)據(jù)庫(kù)的方法7. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. XML入門(mén)的常見(jiàn)問(wèn)題(四)10. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享
