【问题标题】:Vuejs require("axios") in .js file, require is not defined [duplicate].js 文件中的 Vuejs 要求(“axios”),要求未定义 [重复]
【发布时间】:2022-01-15 12:38:20
【问题描述】:

我正在使用 vuejs,我正在尝试向我的 API 发出 GET 请求。我正在使用 axios,但我无法导入它,如果我使用 require 导入 axios 我会收到此错误:Uncaught ReferenceError: require is not defined

这是我的 vuetest.js 文件:

const axios = require("axios");

new Vue({
  el: "#rentalsVue",
  data() {
    return {
      info: null,
    };
  },
  mounted() {
    axios.get("/getRentals").then((response) => (this.info = response));
  },
});

这是html:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="rentalsVue">
        {{ info }}
    </div>


    <script src="vuetest.js"></script>
</body>

</html>

【问题讨论】:

    标签: javascript html vue.js axios get


    【解决方案1】:

    这是因为require() 在浏览器/客户端 JavaScript 上不存在。

    您需要使用&lt;script&gt;标签导入Axios,然后才能使用。

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 2019-07-17
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2019-04-15
      相关资源
      最近更新 更多