您好,登錄后才能下訂單哦!
這篇“vue+vuex+koa2開發(fā)環(huán)境怎么搭建”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue+vuex+koa2開發(fā)環(huán)境怎么搭建”文章吧。
第一部分:環(huán)境搭建
vue + vuex環(huán)境
首先是vue + vue-router + vuex的環(huán)境。我們用vue-cli腳手架生成項(xiàng)目,會用vue的同學(xué)對這塊應(yīng)該很熟了。
// 全局安裝腳手架工具 npm i vue-vli -g // 驗(yàn)證腳手架工具安裝成功與否 vue --version // 構(gòu)建項(xiàng)目 vue init webpack 項(xiàng)目名 // 測試vue項(xiàng)目是否運(yùn)行成功 npm run dev
因?yàn)槟_手架生成的vue項(xiàng)目不包含vuex,所以再安裝vuex。
// 安裝vuex npm i vuex --save
koa2環(huán)境
前端項(xiàng)目構(gòu)建好了,就開始構(gòu)建我們的后端服務(wù)。
首先在你的開發(fā)工具(不管是webstorm還是sublime)里新建一個目錄,用來搭建基于koa的web服務(wù)。
在這里,我們不妨給這個目錄起名為koa-demo。
然后執(zhí)行:
// 進(jìn)入目錄 cd koa-demo // 生成package.json npm init -y // 安裝以下依賴項(xiàng) npm i koa npm i koa-router npm i koa-cors
安裝好koa和兩個中間件,環(huán)境就算搭建完成了。
第二部分:示例開發(fā)
搭建環(huán)境是為了使用,所以我們立馬來寫一個demo出來。
demo開發(fā)既是一個練習(xí)如何在開發(fā)環(huán)境中寫代碼的過程,反過來,也是一個驗(yàn)證環(huán)境搭建的對不對、好不好用的過程。
后端接口開發(fā)
本例中,后端我們只提供一個服務(wù),就是給前端提供一個返回json數(shù)據(jù)的接口。代碼中包含注釋,所以直接上代碼。
server.js文件
// server.js文件 let Koa = require('koa'); let Router = require('koa-router'); let cors = require('koa-cors'); // 引入modejs的文件系統(tǒng)API let fs = require('fs'); const app = new Koa(); const router = new Router(); // 提供一個/getJson接口 router .get('/getJson', async ctx => { // 后端允許cors跨域請求 await cors(); // 返回給前端的數(shù)據(jù) ctx.body = JSON.parse(fs.readFileSync( './static/material.json')); }); // 將koa和兩個中間件連起來 app.use(router.routes()).use(router.allowedMethods()); // 監(jiān)聽3000端口 app.listen(3000);
這里面用到了一個json文件,在'./static/material.json'路徑,該json文件的代碼是:
// material.json文件 [{ "id": 1, "date": "2016-05-02", "name": "張三", "address": "北京 清華大學(xué)", }, { "id": 2, "date": "2016-05-04", "name": "李四", "address": "上海 復(fù)旦大學(xué)", }, { "id": 3, "date": "2016-05-01", "name": "王五", "address": "廣東 中山大學(xué)", }, { "id": 4, "date": "2016-05-03", "name": "趙六", "address": "廣東 深圳大學(xué)", }, { "id": 5, "date": "2016-05-05", "name": "韓梅梅", "address": "四川 四川大學(xué)", }, { "id": 6, "date": "2016-05-11", "name": "劉小律", "address": "湖南 中南大學(xué)", }, { "id": 7, "date": "2016-04-13", "name": "曾坦", "address": "江蘇 南京大學(xué)", }]
然后我們是用以下命令將服務(wù)啟動
node server.js
測試接口是否良好
打開瀏覽器,輸入http://127.0.0.1:3000/getJson??匆豢错撁嫔鲜欠駥son文件中的json數(shù)據(jù)顯示出來,如果能夠顯示出來,則說明這個提供json數(shù)據(jù)的服務(wù),我們已經(jīng)搭建好了。
前端調(diào)用后端接口示例
為突出重點(diǎn),排除干擾,方便理解。我們的前端就寫一個組件,組件有兩部分:首先是一個按鈕,用來調(diào)用web服務(wù)的getJson接口;然后是一個內(nèi)容展示區(qū)域,拿到后端返回的數(shù)據(jù)以后,將其在組件的這塊區(qū)域顯示出來。
首先我們看組件文件吧
<template> <div class="test"> <button type="button" @click="getJson">從后端取json</button> <div class="showJson">{{json}}</div> </div> </template> <script> import {store} from '../vuex' export default { computed: { json(){ return store.state.json; } }, methods: { getJson(){ store.dispatch("getJson"); } } } </script> <style scoped> .showJson{ width:500px; margin:10px auto; min-height:500px; background-color: palegreen; } </style>
非常簡單,就不多解釋了。
然后看我們的vuex文件。
import Vue from 'vue' import Vuex from 'vuex'; Vue.use(Vuex) const state = { json: [], }; const mutations = { setJson(state, db){ state.json = db; } } const actions = { getJson(context){ // 調(diào)用我們的后端getJson接口 fetch('http://127.0.0.1:3000/json', { method: 'GET', // mode:'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, }).then(function (res) { if(res.status === 200){ return res.json() } }).then(function (json) { //console.log(typeof Array.from(json), Array.from(json)); context.commit('setJson', Array.from(json)); }) } }; export const store = new Vuex.Store({ state: state, mutations: mutations, actions: actions, })
ok, 代碼擼完了。
說說axios
想要把本demo的fetch改為axios方式,要做的工作有以下幾處:
1、安裝axios、在vuex文件引用axios
npm i axios import axios from 'axios'
2、將fetch部分代碼替換為:
const actions = { getJson(context){ axios.get('/json', { method: 'GET', // mode:'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, }).then(function (res) { if(res.status === 200){ return res.data } }).then(function (json) { //console.log(typeof Array.from(json), Array.from(json)); context.commit('setJson', Array.from(json)); }) } };
3、又會遇到跨域,在webpack中修改,路徑config/index.js文件中添加proxyTable項(xiàng)的配置:
proxyTable: { '/json': { target: 'http://127.0.0.1:3000', changeOrigin: true, pathRewrite: { '^/json': '/json' } } },
Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。
以上就是關(guān)于“vue+vuex+koa2開發(fā)環(huán)境怎么搭建”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。