Vuecli 配合后端php

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

在很多情况下前端并不清楚后端与前端怎么协同工作的,下面的例子是后端用的php集成包wamp,

php文件存放在www--appi--index.php

 "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: '',
  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





特别注意路径要写成api/appi/index.php

转载于:https://my.oschina.net/u/2612473/blog/1548448

你可能感兴趣的:(Vuecli 配合后端php)