安装流程

1、终端运行 vue create demo-3 创建一个文件夹 demo-3 (编辑器打开上一目录创建)
2、选择自定义安装
image.png
3、选择安装CSS ( 选择是按空格键 )
image.png
4、安装vue版本2
image.png
5、选择域处理器 选择Less
image.png
6、选择SElint 的那种规范 ( 选择标准规范 )
image.png
7、选择 保存的时候校错 第二个是全部写完在校错 所有选择第一个
image.png
8、放在独立的配置里面
image.png
9、是否将刚才的预设存起来 (n为不存)
image.png
10、安装插件
image.png
image.png

把axios挂载到vue原型上 main.js (不建议使用)

在终端运行 npm i axios -S 安装axios

  1. import axios from 'axios'
  2. // 全局配置 axios 的请求根路径
  3. axios.defaults.baseURL = 'http://www.liulongbin.top:3006'
  4. // 把 axios 挂载到 Vue.prototype 上, 供每个 .vue 组件的实例直接使用
  5. Vue.prototype.$http = axios
  6. // 今后, 在每个 .vue组件中要发起请求, 直接调用 this.$http.xxx
  7. // 但是, 把 axios 挂载到 Vue原型上, 有一个缺点, 不利于 API 接口的复用!!!
  1. <script>
  2. export default {
  3. methods: {
  4. async getInfo () {
  5. const { data: res } = await this.$http.get('/api/get')
  6. console.log(res)
  7. }
  8. }
  9. }
  10. </script>
  1. <script>
  2. export default {
  3. methods: {
  4. async postInfo () {
  5. const { data: res } = await this.$http.post('/api/post', { name: 'zs', age: 20 })
  6. console.log(res)
  7. }
  8. }
  9. }
  10. </script>

把axios挂载到src -> 新建 utils文件 里面的request.js (建议使用)

第一步: 先在终端运行 npm i axios -S 安装axios

  1. import axios from 'axios'
  2. const request = axios.create({
  3. // 指定请求的根路径
  4. baseURL: 'https://www.escook.cn'
  5. })
  6. export default request
  1. <script>
  2. // 导入 request.js
  3. import request from '@/utils/request.js'
  4. export default {
  5. name: 'Home',
  6. data () {
  7. return {
  8. // 页面值
  9. page: 1,
  10. // 每页显示多少条数据
  11. limit: 10
  12. }
  13. },
  14. created () {
  15. this.initArticleList()
  16. },
  17. methods: {
  18. // 封装获取文章列表数据的方法
  19. async initArticleList () {
  20. // 发起 GET 请求, 获取文章的列表数据
  21. const { data: res } = await request.get('/articles', {
  22. // 请求参数
  23. params: {
  24. _page: this.page,
  25. _limit: this.limit
  26. }
  27. })
  28. console.log(res)
  29. }
  30. }
  31. }
  32. </script>

image.png
第二种 调用方法 更加适合 复用
image.png
image.png