可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块。模块只有通过 module.exports 或者 exports 才能对外暴露接口。

  需要注意的是:

  • exportsmodule.exports 的一个引用,因此在模块里边随意更改 exports 的指向会造成未知的错误。所以更推荐开发者采用 module.exports 来暴露模块接口,除非你已经清晰知道这两者的关系。
module.exports = {
    postList: local_database
}

  在需要使用这些模块的文件中,使用 require(path) 将公共代码引入(只能用相对路径).

var postsData = require('../../data/posts-data.js')
onLoad: function () {
    this.data.postList = postsData.postList
}

  

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2021-11-19
  • 2021-04-10
  • 2022-03-10
  • 2021-11-16
猜你喜欢
  • 2021-10-25
  • 2021-08-29
  • 2021-08-19
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案