Vue - 不能在模块外使用导入语句 [英] Vue - Cannot use import statement outside a module

查看:45
本文介绍了Vue - 不能在模块外使用导入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表单.这个表单有一个包含 vue、axios 和 sweetalert 的 package.json.

I´m trying to create a form. This form have a package.json with vue, axios and sweetalert.

"dependencies": {
  "axios": "^0.19.0",
  "vue": "^2.6.10",
  "vue-sweetalert2": "^2.1.1"
}

JS 是

<script>
  import Vue from 'vue';
  import VueSweetalert2 from 'vue-sweetalert2';
  import VueAxios from 'vue-axios';
  import axios from 'axios';
  import 'sweetalert2/dist/sweetalert2.min.css';
  Vue.use(VueSweetalert2, VueAxios, axios);
  var app = new Vue({
    el: '#app',
    methods: {
      guardar: function () {
        axios
          .get('ENDPONT')
          .then(response => {
            console.log(response);
            Vue.swal('Hello Vue world!!!');

          })
          .catch(function (error) {
            console.log(error);
            Vue.swal('Hello Vue world!!!');
          });
      }
    }
  });
</script>

错误很简单:返回不能在模块外使用导入语句".我想导入是错误的,但我是 Vue 新手,不知道什么是正确的方法.

The error is simple: Return 'Cannot use import statement outside a module'. I suppose that import is wrong, but im new in Vue and don't know what is the right way.

推荐答案

您正在尝试在普通脚本标签中使用 import 语句,您只能使用 type="module"但我怀疑如果您继续沿着这条路走下去,您会遇到许多其他问题,因为许多库尚未构建用于 ESM 模块.

You're trying to use an import statement in a normal script tag, you can only do that with type="module" but I suspect you will run into many other issues if you continue down this path as many libraries are not built for use with ESM modules yet.

最好使用 https://cli.vuejs.org/ 生成项目这将创建一个良好的起始基础并编译您的代码并将其放在 /build 文件夹中,以便您部署到您的网络托管.

You'll be better off generating a project with https://cli.vuejs.org/ which will create a good starting base and compile your code and put it in a /build folder for you to deploy to your web hosting.

这篇关于Vue - 不能在模块外使用导入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆