vue cli是一个用于构建基于vue.js项目(类似于java后端像maven那样)的构建工具。
环境准备
安装node.js
配置 npm(类似于maven中的仓库)
1
2
3
4
5
6
7
8
9# 切换为淘宝镜像源
npm config set registry https://registry.npm.taobao.org
# 验证是否切换成功
npm config get registry
# 配置npm依赖下载位置
npm config set cache "d:\nodejs\npm_cache"
npm config set prefix "d:\nodejs\npm_global"
# 查看配置
npm config ls安装 vue cli
1
2# 全局安装 安装在之前配置好的D:\nodejs\npm_global\node_modules目录下
npm install -g vue-cli
使用vue cli创建一个vue项目
创建一个项目名为hello的vue项目
1 | vue init webpack hello |
启动项目
1 | cd hello |
本地访问
项目结构:
1 | hello ------------->项目名 |
开发步骤
1、编写各个组件:主页、用户、商品等等功能模块,并导出
1 | <template> |
2、在index.js中引入并注册组件
1 | import Vue from 'vue' |
3、在App.vue中添加访问路径
1 | <template> |
在vue cli中使用axios
在当前工程下安装axios
1
npm install axios --save-dev
在main.js 中引入axios
1
2
3import axios from 'axios';
Vue.prototype.$http=axios; // 修改 vue内部 $http为 axios发起异步请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<script>
export default {
name: "User",
data() {
return{
users: []
}
},
method:{
},
components:{
},
created(){
this.$http.get("http://rap2api.taobao.org/app/mock/270195/user/findAll").then(res=>{
this.users = res.data.results;
})
}
};
</script>数据渲染
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<template>
<div>
<h1>用户管理</h1>
<a href="">添加用户</a>
<table border="1" cellspacing="0" width="600px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>生日</th>
<th align="center">操作</th>
</tr>
<tr v-for="user in users">
<td v-text="user.id"></td>
<td v-text="user.name"></td>
<td v-text="user.age"></td>
<td v-text="user.birth"></td>
<td align="center"><a href="">修改</a>
<a href="">删除</a></td>
</tr>
</table>
</div>
</template>