Vue, Axios的基本使用,

114 阅读1分钟

Axios的基本使用

            npm install axios 安装
             执行 `GET` 请求
            axios.get('/user?ID=12345')
              .then(function (response) {
                console.log(response);
              })
              .catch(function (error) {
                console.log(error);
              });

            // 可选地,上面的请求可以这样做
            axios.get('/user', {
                params: {
                  ID: 12345
                }
              })
              .then(function (response) {
                console.log(response);
              })
              .catch(function (error) {
                console.log(error);
              });

              执行 `POST` 请求
                axios.post('/user', {
                    firstName: 'Fred',
                    lastName: 'Flintstone'
                  })
                  .then(function (response) {
                    console.log(response);
                  })
                  .catch(function (error) {
                    console.log(error);
                  });

              完整写法
                // 发送 POST 请求
                axios({
                  method: 'post',
                  url: '/user/12345',
                  data: {
                    firstName: 'Fred',
                    lastName: 'Flintstone'
                  }
                });

兄弟组件通信

在main.js上添加$bus

image.png

使用$bus通信 左边名,右边值

image.png bus.bus.on接收,v就是传过来的值

image.png