ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 模块化 ## 模块化 可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块。模块只有通过[`module.exports`](https://developers.weixin.qq.com/miniprogram/dev/reference/api/module.html)或者`exports`才能对外暴露接口。 注意: - `exports`是[`module.exports`](https://developers.weixin.qq.com/miniprogram/dev/reference/api/module.html)的一个引用,因此在模块里边随意更改`exports`的指向会造成未知的错误。所以更推荐开发者采用`module.exports`来暴露模块接口,除非你已经清晰知道这两者的关系。 - 小程序目前不支持直接引入`node_modules`, 开发者需要使用到`node_modules`时候建议拷贝出相关的代码到小程序的目录中,或者使用小程序支持的[npm](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)功能。 ``` <pre class="calibre13">``` <span class="token6">// common.js</span> <span class="token2">function</span> <span class="token3">sayHello</span><span class="token1">(</span>name<span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span><span class="token4">`Hello </span><span><span class="token1">${</span>name<span class="token1">}</span></span><span class="token4"> !`</span></span><span class="token1">)</span> <span class="token1">}</span> <span class="token2">function</span> <span class="token3">sayGoodbye</span><span class="token1">(</span>name<span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span><span class="token4">`Goodbye </span><span><span class="token1">${</span>name<span class="token1">}</span></span><span class="token4"> !`</span></span><span class="token1">)</span> <span class="token1">}</span> module<span class="token1">.</span>exports<span class="token1">.</span>sayHello <span class="token">=</span> sayHello exports<span class="token1">.</span>sayGoodbye <span class="token">=</span> sayGoodbye ``` ``` 在需要使用这些模块的文件中,使用`require`将公共代码引入 ``` <pre class="calibre14">``` <span class="token2">var</span> common <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">'common.js'</span><span class="token1">)</span> <span class="token3">Page</span><span class="token1">(</span><span class="token1">{</span> helloMINA<span class="token1">:</span> <span class="token2">function</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> common<span class="token1">.</span><span class="token3">sayHello</span><span class="token1">(</span><span class="token4">'MINA'</span><span class="token1">)</span> <span class="token1">}</span><span class="token1">,</span> goodbyeMINA<span class="token1">:</span> <span class="token2">function</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> common<span class="token1">.</span><span class="token3">sayGoodbye</span><span class="token1">(</span><span class="token4">'MINA'</span><span class="token1">)</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> ``` ```