当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js path.relative(from, to)用法及代码示例

path.relative(from, to)

历史
版本变化
v6.8.0

在 Windows 上,UNC 路径的前导斜杠现在包含在返回值中。

v0.5.0

添加于:v0.5.0


参数

path.relative()方法根据当前工作目录返回fromto的相对路径。如果 fromto 都解析到相同的路径(在每个都调用 path.resolve() 之后),则返回零长度字符串。

如果零长度字符串作为 fromto 传递,则将使用当前工作目录而不是零长度字符串。

例如,在 POSIX 上:

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// Returns: '../../impl/bbb'

在 Windows 上:

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// Returns: '..\\..\\impl\\bbb'

如果 fromto 不是字符串,则会抛出 TypeError

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 path.relative(from, to)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。