WeChat Mini Program Absolute Path require

Posted May 28, 20201 min read

https://segmentfault.com/a/11...

Before referring to the above article, the introduction of absolute paths can be achieved, but in vscode, the require function cannot automatically prompt the path and the required module cannot automatically prompt

To solve this problem


  1. Create a jsconfig.json file in the root directory

    {
    "compilerOptions":{

     "target":"ES6",
     "baseUrl":".",
     "paths":{
       "/*":["./*"]
     }

    }
    }


  1. At the beginning of the js file using the require function, add

    const app = getApp();
    (function() {require = app.require;})();


  1. Use

    const CommonApi = require("/api/common"); //Automatic prompt path
    CommonApi.CallApi1(); //Can automatically prompt the method, can be used with d.ts