當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。