Vuecli 配合後端php

在不少狀況下前端並不清楚後端與前端怎麼協同工做的,下面的例子是後端用的php集成包wamp,javascript

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

<?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);

?>

調用後臺的數據採用的axioscss

首先安裝axios,npm install axios --savehtml

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主要配置 vue

proxyTable: {
         '/api': {
          target: 'http://127.0.0.1:80',
          changeOrigin: true,
          pathRewrite: {
           '^/api': '/'
          }
         }
    },java

// 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.vuenode

<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.phpwebpack

相關文章
相關標籤/搜索