require
page.js、自定义组件 JS 都可以 require,规则与任务脚本相同。
完整说明见 模块与 require。本页只给界面侧最短示例。
最短示例
javascript
// common/hello.js
module.exports = {
text: '你好'
};javascript
// pages/home/page.js
let hello = require('../../common/hello.js');
Page({
data: {
hint: hello.text
}
});| 当前文件 | 引入 | 写法 |
|---|---|---|
pages/home/page.js | common/hello.js | require('../../common/hello.js') |
pages/home/page.js | 同目录 helper.js | require('./helper.js') |
优先 ./、../。导出只用 module.exports,禁止 import / export。与 Engines.executeScript 不同:后者路径相对项目根。
