javascript - 如何从es6模块javascript中导入的文件中获取脚本url?
问题描述
如果一些 JS 代码有这个
import("path/to/file.js")
然后 file.js 有这个
export default async function() {
// I want to get "path/to" here
return {};
}
如何获取 where file.js
is 的目录?
解决方案
export default async function() {
var current_file = import.meta.url;
var dir_path = import.meta.url.substring(0, import.meta.url.lastIndexOf("/"));
return {};
}
在此处查看兼容性https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta
推荐阅读
- python - 初始化和验证表单后更改 BoundField 值
- javascript - 使用 :hidden 选择器删除隐藏元素
- python-3.x - 如何在程序结束时关闭python进程
- spring - 使用 Hibernate JPA 处理工作单元的问题
- sockets - 带有raspberrypi的android设备的蓝牙通信-连接被拒绝时出现错误
- c - 子进程和父进程之间的同步 c
- typescript - Typescript 中的字符串验证
- node.js - 软件包在 NMP 安装时被删除
- javascript - 如何使用 Vue.js 下载图像而不显示它们?
- python - 序列化程序调用显示 TypeError:“ListSerializer”类型的对象不是 JSON 可序列化的?