How to import a variable from a JS file in another JS file that uses Vue CDN? - Uncaught SyntaxError: Unexpected identifier

How to import a variable from a JS file in another JS file that uses Vue CDN? - Uncaught SyntaxError: Unexpected identifier

我是 Vue 的新手,一直在尝试导入一个变量,该变量由 API 键组成,这是我的 app.js 使用 Vue CDN 所必需的。 但是我收到了这个错误:

Uncaught SyntaxError: Unexpected identifier app.js:1

其他一切正常,只有我在导入时遇到问题。

我的代码预览:

//---config.js---


export const key = 'someKey';


//---app.js---

import key from './config.js'

new Vue({
  ...,
  components: {
    key
  },
  ...

P.S。有没有办法让它在不使用 Vue CLI 的情况下工作?

您在哪里使用导入导出语句? (Chrome、Firefox 等)。要了解 import export 语句在哪里工作,您应该检查浏览器兼容性。

如果您想使用 import/export,您应该在 html 的脚本标签中添加 type="module"。像这样。

<script type="module" src="index.js"></script>

并且当您在导入时使用导出而不是默认导出时使用:

import {key} from './config.js'