vue的學習之路 vue-cli與axios

來源:互聯網
上載者:User

標籤:.post   password   imp   tor   www   admin   搭建   fun   安裝   

1、搭建一個vue-cli搭建一個項目

(1) 安裝vue-cli

  npm install vue-cli -g

(2) vue init webpack 項目名

  根據需要選擇設定

(3) npm install

  安裝配置(安裝配置時留意一下當前位置是不是在項目裡,不然會報錯:找不到設定檔package.json)

(4) npm run dev

  本地運行

 

 

2、用axios與後端進行資料傳遞

(1) 安裝axios

  npm install axios 

(2) 在main.js中引入axios

  import axios form ‘axios‘

(3) 將axios掛在到vue.prototype.$http

  Vue.prototype.$http = axios;

(4) 在config/index.js中修改配置,定義全域變數、設定跨域狀態

dev: {

// Paths
assetsSubDirectory: ‘static‘,
assetsPublicPath: ‘/‘,
proxyTable: {
‘/api‘: {
target: ‘https://www.baidu.com/public‘,
changeOrigin: true, // 在本地會建立一個虛擬服務端,服務端和服務端進行資料的互動就不會有跨域問題
pathRewrite: {
‘^/api‘: ‘‘ //替換targe中的請求地址
}
}
},

(5)提供者
  this.$http.post(‘/api/login‘,
{
name: "admin",
password: "123456"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
(6)配置好index.js後要重新啟動

vue的學習之路 vue-cli與axios

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.