美文网首页
vue3 引入正确安装

vue3 引入正确安装

作者: 小李不小 | 来源:发表于2021-05-28 23:27 被阅读0次

    这种模式安装aioxs ,会报错

    npm install axios --save
    

    报错内容

    Error: Cannot find module 'vue-loader-v16/package.json'
    Require stack:
    - C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\config\base.js
    - C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\Service.js
    - C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\bin\vue-cli-service.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
        at Function.Module._load (internal/modules/cjs/loader.js:864:27)
        at Module.require (internal/modules/cjs/loader.js:1044:19)
        at require (internal/modules/cjs/helpers.js:77:18)
        at C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\config\base.js:114:23       
        at C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\Service.js:236:40
        at Array.forEach (<anonymous>)
        at Service.resolveChainableWebpackConfig (C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\Service.js:236:26)
        at Service.resolveWebpackConfig (C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\Service.js:240:48)
        at PluginAPI.resolveWebpackConfig (C:\Users\star小鹏王\Desktop\vue3ts\vuet\node_modules\@vue\cli-service\lib\PluginAPI.js:132:25)
    npm ERR! code ELIFECYCLE
    npm ERR! vuet@0.1.0 serve: `vue-cli-service serve`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the vuet@0.1.0 serve script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\star小鹏王\AppData\Roaming\npm-cache\_logs\2021-05-28T15_22_31_814Z-debug.log
    PS C:\Users\star小鹏王\Desktop\vue3ts\vuet> npm install axios -S  
    npm WARN postcss-modules@4.0.0 requires a peer of postcss@^8.0.0 but none is installed. You must install peer dependencies yourself.
    npm WARN icss-utils@5.1.0 requires a peer of postcss@^8.1.0 but none is installed. You must install peer dependencies yourself.
    

    正确安装

    npm install axios -S
    
    image.png

    .http.js

    import axios from "axios";
    import qs from "qs";
    import {
      Dialog
    } from "vant";
    
    // axios.defaults.baseURL = 'http://hp.suoluomei.cn/index.php'  //正式
    axios.defaults.baseURL = 'http://hp.suoluomei.cn/index.php' //测试
    
    //post请求头
    axios.defaults.headers.post["Content-Type"] =
      "application/x-www-form-urlencoded;charset=UTF-8";
    //设置超时
    axios.defaults.timeout = 10000;
    
    axios.interceptors.request.use(
      config => {
        return config;
      },
      error => {
        return Promise.reject(error);
      }
    );
    
    axios.interceptors.response.use(
      response => {
        if (response.status == 200) {
          return Promise.resolve(response);
        } else {
          return Promise.reject(response);
        }
      },
      error => {
        Dialog.alert({
          title: "提示",
          message: "网络请求失败,请刷新重试"
        });
      }
    );
    export default {
      post(url, data) {
        return new Promise((resolve, reject) => {
          axios({
              method: 'post',
              url,
              data: qs.stringify(data),
            })
            .then(res => {
              resolve(res.data)
            })
            .catch(err => {
              reject(err)
            });
        })
      },
    
      get(url, data) {
        return new Promise((resolve, reject) => {
          axios({
              method: 'get',
              url,
              params: data,
            })
            .then(res => {
              resolve(res.data)
            })
            .catch(err => {
              reject(err)
            })
        })
      }
    };
    
    

    request.js

    import http from './http'
    const api_key = '233d4cad34c34d343785s34dw3er3ds3234esd4'
    let sign = process.env.VUE_APP_SIGN
    if (process.env.NODE_ENV === 'production') {
      sign = localStorage.getItem("sign")
    } else {
      sign = '17a1920fdc3c3c25d1bf7c6dd724f5cf'
    }
    
    const post = (url, data = {}) =>
      http.post(
        url,
        Object.assign({
            api_key: api_key,
            sign,
          },
          // {
          //   data: Object.assign({
          //       role_id: role_id //如果需要再包层data就这样写
          //     },
          //     data
          //   )
          // }
          data
        )
      )
    const get = (url, params = {}) =>
      http.get(
        url,
        Object.assign({
            api_key: api_key,
            sign
          },
          params
        )
      )
    
    // 接口说明
    const getFissionCourseList = data => post('getFissionCourseList', data)
    
    const getGetrequs = params => get('getFissionCourseList', params)
    //接口导出
    export {
      getFissionCourseList,
      getGetrequs
    }
    

    页面引入使用

    <template>
      <div class="box">
        <div class="demo">欢迎来到VUE3</div>
        <van-button type="success">成功按钮</van-button>
      </div>
    </template>
    
    <script>
    import { getFissionCourseList, getGetrequs } from "../../utils/request";
    export default {
      data() {
        return {
          date: "",
        };
      },
      mounted() {
        this.getFissionCourseList();
        this.getGetrequs();
      },
      methods: {
        async getFissionCourseList() {
          await getFissionCourseList({ t35: 333 }).then((res) => {
            console.log(res);
          });
        },
        async getGetrequs() {
          await getGetrequs({ t35: 333 }).then((res) => {
            console.log(res);
          });
        },
      },
    };
    </script>
    
    <style scoped>
    @import "./index.less";
    </style>
    
    

    相关文章

      网友评论

          本文标题:vue3 引入正确安装

          本文链接:https://www.haomeiwen.com/subject/bvwesltx.html