


Detailed explanation of examples of using third-party library Immutable.js in WeChat applet
Dec 29, 2016 pm 04:35 PM前言
Immutable JS 提供一個(gè)惰性 Sequence,允許高效的隊(duì)列方法鏈,類似 map 和 filter ,不用創(chuàng)建中間代表。immutable 通過惰性隊(duì)列和哈希映射提供 Sequence, Range, Repeat, Map, OrderedMap, Set 和一個(gè)稀疏 Vector。
微信小程序無法直接使用require( 'immutable.js' )進(jìn)行調(diào)用,需要對(duì)下載的Immutable代碼進(jìn)行修改,才能使用。
原因分析
Immutable使用了UMD模塊化規(guī)范
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Immutable = factory()); }(this, function () { 'use strict';var SLICE$0 = Array.prototype.slice; .... }));
UMD的實(shí)現(xiàn)很簡(jiǎn)單,先判斷是否支持Node.js(CommonJS)模塊規(guī)范,存在則使用Node.js(CommonJS)方式加載模塊。再判斷是否支持AMD,存在則使用AMD方式加載模塊。前兩個(gè)都不存在,則將模塊公開到全局。
exports、module必須都有定義,才能以CommonJS加載模塊。通過測(cè)試,微信小程序運(yùn)行環(huán)境exports、module并沒有定義。
解決方法
修改Immutable代碼,注釋原有模塊導(dǎo)出語句,使用module.exports = factory() 強(qiáng)制導(dǎo)出
(function(global, factory) { /* typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Immutable = factory()); */ module.exports = factory(); }(this, function() {
使用Immutable.js
//index.js var Immutable = require( '../../libs/immutable/immutable.modified.js' ); //獲取應(yīng)用實(shí)例 var app = getApp(); Page( { onLoad: function() { //console.log('onLoad'); var that = this; var lines = []; lines.push( "var map1 = Immutable.Map({a:1, b:2, c:3});" ); var map1 = Immutable.Map({a:1, b:2, c:3}); lines.push( "var map2 = map1.set('b', 50);" ); var map2 = map1.set('b', 50); lines.push( "map1.get('b');" ); lines.push(map1.get('b')); lines.push( "map2.get('b');" ); lines.push(map2.get('b')); this.setData( { text: lines.join( '\n' ) }) } })
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望能對(duì)大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
更多Detailed explanation of examples of using third-party library Immutable.js in WeChat applet相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
