vue2.0+webpack+vuerouter+vuex+axios构建项目基础

前言

之前写了vue的基础篇,但是其实vue的官网,对这些基础相当详细,看了
https://www.jianshu.com/p/080875cc260f这篇文章后可以按照官方网站把基础的都学了
vue官网地址:https://cn.vuejs.org/
本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础

步骤
1、全局安装webpack,命令
npm install webpack -g

注意,webpack升级到4舍弃了不少组件,之前有次使用淘宝镜像丢失了不少模块,所以webpack大家尽量使用npm装。

2、安装vue脚手架
npm install vue-cli -g
3、运行cmd(开始-运行-cmd-回车)

比如你的目录要安装在E盘,在命令面板中就输入"e:"然后回车
cd到项目的指定目录

4、根据模板创建项目,命令
vue init webpack-simple 工程名字<工程名字不能用中文>

Project name 输入项目名称直接回车默认,其他的直接回车。

5、安装依赖
npm install

创建好的类目如下图所示


vue2.0+webpack+vuerouter+vuex+axios构建项目基础_第1张图片
image.png
6、运行第一个vue项目,命令
npm run dev
vue2.0+webpack+vuerouter+vuex+axios构建项目基础_第2张图片
image.png

运行完后会运行项目,并打开浏览器展示这个例子项目

7、安装vue-router,vuex,axios

axios相当于ajax,之前是用vue-resourse,不过现在这个模块不维护了,基本使用axios
vue-router是vue的路由
vuex是vue的状态管理,方便组件间通信
安装命令

npm install vue-router vuex axios --save-dev

会保存到package.json文件里,当代码上传别人下载后,可以使用npm install一次性把依赖全部装完

8、书写第一个组件

在src目录下新建一个component文件夹,新建组件文件firstcomponent.vue
firstcomponent.vue







count2就是main.js里面的count

12、axios的使用

axios的使用格式为:
axios.get('/user',{
params:{
ID:12345
}
})
.then(function(response){
console.log(response);
})
.catch(function(err){
console.log(err);
});
get没有区别,但是post请求时后台接收不到数据
简单的给下我封装的axios解决post的方法

import esPromise from 'es6-promise';
import axios from 'axios';
import qs from 'qs';

esPromise.polyfill();

axios.defaults.timeout = 30000;  //设置超时时间30秒
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';           //配置请求头

axios.defaults.baseURL = 'http://172.1.1.1';

 //POST传参序列化
axios.interceptors.request.use((config) => {
    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
     console.log("错误的传参");
    return Promise.reject(error);
});

class Common{
    constructor(){

        }
    }
    ajaxLoading(){
        let loading = document.createElement("div");
        loading.setAttribute("id","loading");
        loading.setAttribute("class","mask-loading");
        let myBody = document.getElementsByTagName("body")[0];
        myBody.appendChild(loading);
        loading.innerHTML = '
加载中
'; } ajaxLoadingStop(){ let loading = document.getElementById("loading"); loading .parentNode.removeChild(loading ); } ajax(api,data,callBack){ let myData = data; let self = this; if(api.type == "get"){ self.ajaxLoading(); axios.get(api.url,{ params:myData.list }).then((response)=>{ self.ajaxLoadingStop(); if (response.data.ResultCode == 6666) { callBack && callBack(response.data); } else { prompt.tip(response.data.Message); } }).catch((error)=>{ self.ajaxLoadingStop(); if(error && error.message && error.message.indexOf('timeout')!=-1){ prompt.tip("访问超时"); }else{ prompt.tip("服务器通信异常"); } }) }else{ self.ajaxLoading(); new Promise((resolve, reject) => { axios.post(api.url,myData.list).then((response)=>{ self.ajaxLoadingStop(); if (response.data.ResultCode == 6666) { callBack && callBack(response && response.data) } else { prompt.tip(response.data.Message); } }).catch((error)=>{ console.log(error); self.ajaxLoadingStop(); if(error && error.message && error.message.indexOf('timeout')!=-1){ prompt.tip("访问超时"); }else{ prompt.tip("服务器通信异常"); } }) }) } } } export default Common;

这只是最简单的例子,供给大家入门,能搭出来后面就容易学了

你可能感兴趣的:(vue2.0+webpack+vuerouter+vuex+axios构建项目基础)