1. 在 config/index.js 配置文件中配置proxyTablephp
'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/webpack for documentation. const path = require('path') module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/apis': { // 測試環境 target: 'http://www.thenewstep.cn/', // 接口域名 changeOrigin: true, //是否跨域 pathRewrite: { '^/apis': '' //須要rewrite重寫的, } } }, // Various Dev Server settings host: 'localhost', // can be overwritten by process.env.HOST port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: true, notifyOnErrors: true, poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- /** * Source Maps */ // https://webpack.js.org/configuration/devtool/#development devtool: 'cheap-module-eval-source-map', // If you have problems debugging vue-files in devtools, // set this to false - it *may* help // https://vue-loader.vuejs.org/en/options.html#cachebusting cacheBusting: true, cssSourceMap: true }, build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: '/', /** * Source Maps */ productionSourceMap: true, // https://webpack.js.org/configuration/devtool/#production devtool: '#source-map', // 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 } }
2. fetch實現跨域請求css
在根組件App.vue裏面發送請求html
// 1. 模板:html結構 有且只有一個根標籤 <template> <div id="app"> <!-- 使用路由實現跳轉 注意:to裏面寫的是路由的名字 --> <ul> <li><router-link to="/">home</router-link></li> <li><router-link to="/Users">Users</router-link></li> </ul> <!-- 進入首頁默認加載的路由 --> <router-view></router-view> </div> </template> //2. 行爲:處理邏輯 <script> export default { name: 'App',//組件App.vue的名字 data () { return { } }, //實現跨域請求 created(){ //fetch實現跨域請求 fetch("/apis/test/testToken.php",{ method:"POST", headers:{ token:"f4c902c9ae5a2a9d8f84868ad064e706" }, body:JSON.stringify({username:"lgs",password:"123"}) }).then(result=>{ // console.log(result) //解析數據 return result.json() }).then(data =>{ //打印數據 console.log(data); }) } } </script> //3. 樣式:解決樣式 <style scoped> h1 { color:purple; } </style>
響應結果:vue
3. axios實現跨域請求webpack
3.1 中止項目,安裝axiosnpm install axios,而後重啓項目npm run devios
3.2 在main.js裏面引入axios,配置全局使用axios,設置token和請求頭git
// 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' //導入vue import VueRouter from 'vue-router' //引入路由 import VueResource from 'vue-resource' //引入vue-resource import axios from 'axios' //引入axios import App from './App' //導入根組件App.vue import Users from './components/Users'//導入組件Users.vue import Home from './components/Home'//導入組件Users.vue //全局使用axios Vue.prototype.$axios = axios //設置token axios.defaults.headers.common['token'] = "f4c902c9ae5a2a9d8f84868ad064e706" //設置請求頭 axios.defaults.headers.post["Content-type"] = "application/json" Vue.config.productionTip = false //引入路由後使用路由,這樣就能夠在任何組件中使用路由了 Vue.use(VueRouter) //引入vue-resource後使用vue-resource,這樣就能夠在任何組件中使用http了 Vue.use(VueResource) //配置路由 const router = new VueRouter( { routes : [ //配置路由跳轉到Home這個組件 {path:"/",component:Home}, //配置路由跳轉到Users.vue這個組件 {path:"/Users",component:Users} ], //去掉地址欄的"/#/" mode : "history" } ) /* eslint-disable no-new */ new Vue({ //實例化一個vue對象 router,//使用路由 el: '#app', //index.html的根元素app components: { App },//註冊根組件App.vue才能使用 template: '<App/>'//VUE模板使用,能夠是組件、html標籤等 })
3.2 在根組件App.vue裏面發送請求github
// 1. 模板:html結構 有且只有一個根標籤 <template> <div id="app"> <!-- 使用路由實現跳轉 注意:to裏面寫的是路由的名字 --> <ul> <li><router-link to="/">home</router-link></li> <li><router-link to="/Users">Users</router-link></li> </ul> <!-- 進入首頁默認加載的路由 --> <router-view></router-view> </div> </template> //2. 行爲:處理邏輯 <script> export default { name: 'App',//組件App.vue的名字 data () { return { } }, //實現跨域請求 created(){ //fetch實現跨域請求 // fetch("/apis/test/testToken.php",{ // method:"POST", // headers:{ // token:"f4c902c9ae5a2a9d8f84868ad064e706" // }, // body:JSON.stringify({username:"lgs",password:"123"}) // }).then(result=>{ // // console.log(result) // //解析數據 // return result.json() // }).then(data =>{ // //打印數據 // console.log(data); // }) //axios實現跨域請求 this.$axios.post("/apis/test/testToken.php",{ username:"lgs",password:"123" }).then(data=>{ console.log(data) }) } } </script> //3. 樣式:解決樣式 <style scoped> h1 { color:purple; } </style>
響應結果:web