vue-wechat
TypeScript icon, indicating that this package has built-in type declarations

1.6.1 • Public • Published

vue-wechat

Wechat sdk for Vue.js.

install

npm i vue-wechat -s

Import vue-wechat before create Vue instance:

import Wechat from 'vue-wechat';
 
Vue.use(Wechat);
 
// ...
new Vue({
  render: h => h(App),
}).$mount('#app');

Import specific version of wechat sdk:

import Wechat from 'vue-wechat/1.2.0';
 
Vue.use(Wechat);
 
// ...
new Vue({
  render: h => h(App),
}).$mount('#app');

Import original wechat sdk object:

import { wechat } from 'vue-wechat';
// import { wx } from 'vue-wechat';
// import { wechat } from 'vue-wechat/1.2.0';
 
wechat.previewImage({ urls: [''], current: '' });

usage

Global use.

import Vue from 'vue';
 
Vue.wechat.config({
  debug: info.debug,
  appId: info.appId,
  timestamp: info.timestamp,
  nonceStr: info.nonceStr,
  signature: info.signature,
  jsApiList: info.jsApiList,
});

Use inside component.

// ...
mounted() {
  this.$wechat.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: this.onWechatFileChoose,
  });
},
methods: {
  onWechatFileChoose({ localIds }) {
    if (window.__wxjs_is_wkwebview) {
      let i = 0;
      const infos = [];
      const next = () => {
        if (< localIds.length) {
          this.$wechat.getLocalImgData({
            localId: localIds[i],
            success: ({ localData }) => {
              infos.push({ dataUrl: localData });
              i += 1;
              next();
            },
          });
        } else {
          this.insertInfo(infos);
        }
      };
      next();
    } else {
      this.insertInfo(localIds.map(localId => ({ src: localId, localId })));
    }
  },
  insertInfo(rawInfos) {
    console.log(rawInfos);
  },
},
// ...

Package Sidebar

Install

npm i vue-wechat

Weekly Downloads

3

Version

1.6.1

License

MIT

Unpacked Size

6.19 kB

Total Files

13

Last publish

Collaborators

  • tinymins