尝试将 moment.js 作为 es6 模块导入。正在使用最新的 Chrome 版本。
基于讨论here , 我尝试了 src 路径 (es6)
import * as moment from './node_modules/moment/src/moment'
虽然跳过 .js 似乎对那个线程上的每个人都很好,但我无法让它工作。但这行得通
import * as moment from './node_modules/moment/src/moment.js'
然而,请求最终失败了,因为 import 始终试图在没有 js 扩展的情况下加载其依赖项
GET http://127.0.0.1:8083/node_modules/moment/src/lib/utils/hooks net::ERR_ABORTED 404 (Not Found) moment.js:22
GET http://127.0.0.1:8083/node_modules/moment/src/lib/moment/moment net::ERR_ABORTED 404 (Not Found) moment.js:26
GET http://127.0.0.1:8083/node_modules/moment/src/lib/moment/calendar net::ERR_ABORTED 404 (Not Found) moment.js:39
GET http://127.0.0.1:8083/node_modules/moment/src/lib/locale/locale net::ERR_ABORTED 404 (Not Found) moment.js:46
GET http://127.0.0.1:8083/node_modules/moment/src/lib/duration/duration net::ERR_ABORTED 404 (Not Found) moment.js:48
GET http://127.0.0.1:8083/node_modules/moment/src/lib/units/units net::ERR_ABORTED 404 (Not Found) moment.js:50
GET http://127.0.0.1:8083/node_modules/moment/src/lib/utils/is-date net::ERR_ABORTED 404 (Not Found)
我所有的代码都是一个带有这个脚本标签的空白 index.html
<script type="module" src='./main.js'></script>
还有一个 main.js 文件
import * as _ from './node_modules/underscore/underscore.js';
import * as moment from './node_modules/moment/src/moment.js';
Underscore.js 导入并工作正常。问题仅在于片刻。 我究竟做错了什么?另外,为什么我无法在不指定 .js 扩展名的情况下加载其中任何一个,而显然 ithub 线程上的那些人已经能够做到这一点
import * as moment from 'moment'
最佳答案
我在使用 ES6 时遇到了类似的问题。你可以试试这种方式导入moment
import moment from "moment";
关于javascript - 无法从支持的浏览器导入 es6 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843867/