• 569 node内置模块path



    path常见的API


    在webpack中的使用

    在webpack中获取路径或者起别名的地方也可以使用


    01_路径的演练.js

    const path = require('path');
    
    const basePath = '/User/why';
    const filename = 'abc.txt';
    
    // const path = basePath + "/" + filename;
    
    const filepath = path.resolve(basePath, filename);
    console.log(filepath);
    
    

    02_path其他方法.js

    const path = require('path');
    
    // 1.获取路径的信息
    // const filepath = '/User/why/abc.txt';
    // console.log(path.dirname(filepath)); // /User/why
    // console.log(path.basename(filepath)); // abc.txt
    // console.log(path.extname(filepath)); // .txt
    
    
    // 2.join路径拼接
    const basepath = '../User/why';
    const filename = './abc.txt';
    const othername = './why.js';
    
    const filepath1 = path.join(basepath, filename);
    // console.log(filepath1); // ..Userwhyabc.txt
    
    // 3.resolve路径拼接
    // resolve会判断拼接的路径字符串中,是否有以/或./或../开头的路径
    // const filepath2 = path.resolve(basepath, filename, othername);
    // console.log(filepath2); // F:前端why
    odekejianday01_24Userwhyabc.txtwhy.js
    
    const basepath2 = '/User/coderwhy';
    // const filename2 = '/why/abc.txt'; // /why/abc.txt
    // const filename2 = './why/abc.txt'; // /User/coderwhy/why/abc.txt
    // const filename2 = 'why/abc.txt'; // /User/coderwhy/why/abc.txt
    
    const filename2 = '../why/abc.txt'; // /User/coderwhy/why/abc.txt
    
    const result = path.resolve(basepath2, filename2);
    console.log(result);
    
    

    03.使用esmodule加载.mjs

    import path from 'path';
    
    const basepath = '../User/why';
    const filename = '/abc.txt';
    const othername = '/why.js';
    
    const filepath1 = path.join(basepath, filename);
    console.log(filepath1);
    


  • 相关阅读:
    1.4redis小结--队列在抢购活动的实现思路
    1.3redis小结--配置php reids拓展
    redis小结 1-2
    redis小结 1-1
    pandas学习小记
    Python简单算法的实现
    python编码
    ThinkPHP中的__initialize()和类的构造函数__construct()
    js正则常用方法
    总结了下PHPExcel官方读取的几个例子
  • 原文地址:https://www.cnblogs.com/jianjie/p/14232273.html
Copyright © 2020-2023  润新知