前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vuecli 配合后端php 原

Vuecli 配合后端php 原

作者头像
tianyawhl
发布2019-04-04 11:13:29
2K1
发布2019-04-04 11:13:29
举报
文章被收录于专栏:前端之攻略前端之攻略

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

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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
// 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': '/'           }          }     },

代码语言:javascript
复制
// 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

代码语言:javascript
复制
<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({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/10/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档