小程序插件开发

2020-07-09  本文已影响0人  RadishHuang

官网小程序插件开发地址

module.exports = {
  resetConfig(config = null) {
    if (config) {
      this.config = config;
    }
  },
  config: null,
  sayHello: function () {
    console.log('Hello plugin!')
  },
  answer: 42
}

测试了下index.js的使用,发现如果在外部调用resetConfig方法后,设置的config可以在插件里面全局通用。无论在外部的小程序,或者是插件内容,都能拿到设置后的config

测试数据
{
  "publicComponents": {
    "empty-component": "components/empty/index"
  },
  "pages": {
    "hello-page": "pages/hello-page"
  },
  "main": "index.js"
}
{
  "pages": [
    "pages/index/index",
    "pages/index2/index"
  ],
  "plugins": {
    "radish-plugin": {
      "version": "dev",
      "provider": "wxbdbc93731a2557d9"
    }
  },
  "sitemapLocation": "sitemap.json"
}
{
  "usingComponents": {
    "empty-component": "plugin://radish-plugin/empty-component"
  }
}
<!-- 跳转到插件的某个页面 -->
<navigator id="nav" url="plugin://radish-plugin/hello-page">
  Go to Plugin page
</navigator>


<view class="content">
  <view bindtap="showIndex">to === index2</view>
  <!-- 引入插件的某个组件 -->
  <empty-component show="{{show}}"></empty-component>
</view>
const plugin = requirePlugin("radish-plugin");

Page({
  data: {
    // items: [],
    // currentItem: 0
    show: false,
  },
  onLoad() {
    
    plugin.resetConfig({
      title: 'program title',
      image: '/miniprogram/pages/index/1.jpeg',
      detail: 'pages/index/index'
    });
    setTimeout(()=>{
      this.setData({
        show: true
      })
    }, 2000)
  },
  addItem: function () {
    // this.data.items.push(this.data.currentItem++);
    // this.setData({
    //   items: this.data.items,
    //   currentItem: this.data.currentItem
    // });
  },
  showIndex() {
    wx.navigateTo({
      url: "/pages/index2/index"
    });
      
  }
});

,总结下插件的配置

上一篇 下一篇

猜你喜欢

热点阅读