在很多情况下前端并不清楚后端与前端怎么协同工作的,下面的例子是后端用的php集成包wamp,
php文件存放在www--appi--index.php
<?php
header("Content-Type: application/json;charset=utf-8");
$success = true;
$data = array('a' => "this is from backend11112",'e' => "54");
// 返回json数据,或者字符串,数字。
$res = array('success' =>$success, 'data' =>$data);
echo json_encode($res);
?>
调用后台的数据采用的axios
首先安装axios,npm install axios --save
main.js引用axios
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import router from './router'
//引用API文件
import axios from 'axios'
//import api from './api/index.js'
//将API方法绑定到全局
Vue.prototype.$ajax = axios
//引用工具文件
import utils from './utils/index.js'
//将工具方法绑定到全局
Vue.prototype.$utils=utils
Vue.use(ElementUI)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app1',
router,
template: '<App/>',
components: { App }
})
config文件夹下的index.js主要配置
proxyTable: { '/api': { target: 'http://127.0.0.1:80', changeOrigin: true, pathRewrite: { '^/api': '/' } } },
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/dist/',
productionSourceMap: false,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://127.0.0.1:80',
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
// '/api/v1/**':{
// target:'https://cnodejs.org',//你接口的域名
// secure:false,
// changeOrigin:true,
// }
},
// '/api/v1/**'也可以写成'/api'
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
index.vue
<template>
<div>
hi everyone
<h2>{{msg}}</h2>
</div>
</template>
<script>
export default {
data() {
return {
msg: ""
}
},
created() {
this.getData()
},
methods: {
getData() {
let _this = this;
_this.$ajax.get('api/appi/index.php')
.then(function(response) {
console.log(response)
_this.msg = response.data.data.a
//response.data是返回请求的json对象
})
.catch(function(error) {
console.log(error);
});
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
ul>li {
margin-bottom: 30px;
}
.time {
margin-bottom: 10px;
}
</style>
特别注意路径要写成api/appi/index.php
(adsbygoogle = window.adsbygoogle || []).push({});