溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序中框架的示例分析

發(fā)布時間:2021-06-10 12:05:44 來源:億速云 閱讀:150 作者:小新 欄目:移動開發(fā)

這篇文章給大家分享的是有關(guān)微信小程序中框架的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

快速了解微信小程序的使用,一個根據(jù)小程序的框架開發(fā)的todos app

微信官方已經(jīng)開放微信小程序的官方文檔和開發(fā)者工具。前兩天都是在看相關(guān)的新聞來了解小程序該如何開發(fā),這兩天官方的文檔出來之后,趕緊翻看了幾眼,重點了解了一下文檔中框架與組件這兩個部分,然后根據(jù)簡易教程,做了一個常規(guī)的todo app。這個app基于微信小程序的平臺,實現(xiàn)了todo app的常規(guī)功能,同時為了讓它更接近實際的工作場景,也用到了loading與toast這兩個組件來完成一些操作的交互與反饋。這個平臺給我的直觀感受是,技術(shù)層面,它跟vue有相似性,但是遠沒有vue強大;開發(fā)時候的思路,不像vue,反倒覺得比較像backbone。所以要是使用過backbone,vue等mvc,mvvm框架的人,會覺得這個平臺上手很容易。本文主要介紹這個todo app實現(xiàn)的一些要點。

先補充下本文相關(guān)的資料:

官方文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/index.html

官方開發(fā)者工具下載:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html

本文todo app的功能演示:

微信小程序中框架的示例分析

注:需長按todo的text,才能直接編輯。因為是在手機端,所以不能使用雙擊事件來進行編輯,改成了長按事件。小程序的平臺也沒有提供雙擊事件的綁定。

相關(guān)源碼:https://github.com/liuyunzhuge/blog/tree/master/todos/wx

如果你想在本地運行這個項目,需要先安裝開發(fā)者工具,按照文檔中簡易教程的描述,先建好一個項目;
建完之后,開發(fā)者工具就會打開這個項目;
接著在磁盤上,找到建好的項目的文件夾,把里面的內(nèi)容都刪掉,把上面源碼文件夾下的文件都粘貼進去;
然后重新打開開發(fā)者工具,先進入到編輯頁簽,然后點擊編譯按鈕,就會直接進入到調(diào)試界面,查看app的功能:

微信小程序中框架的示例分析

下面來介紹下這個app開發(fā)的要點:

1. 這個app的目錄結(jié)構(gòu)以及配置等就不詳細介紹了,這些在文檔-框架部分都有很詳細的描述。這個平臺里面沒有html和css,取而代之的是wxml和wxss。wxss跟css幾乎沒有區(qū)別,缺點就是不如css強大,支持的選擇器有限。但是好處是由于只有微信這一個平臺,所以幾乎沒有兼容性問題,能夠使用標準的,更新的css技術(shù)。wxml里面只能用平臺提供的那些組件的標簽,html的標簽不能直接用,各個組件的在wxml的使用方式,都可以在文檔-組件這一部分找到說明的示例。所以實際上wxml跟wxss編寫起來都沒有什么難題。

2. wxml支持以下這些特性:

微信小程序中框架的示例分析

在todo app里面除了模板和引用沒有用到之外,其它的都使用到了,不過沒有使用到每個特性的各個細節(jié),只根據(jù)app的需要選用合適的功能。前幾天看到有文章說,微信小程序可能是基于vue框架來實現(xiàn)的,所以就看了下vue的文檔。對于數(shù)據(jù)綁定,條件渲染,列表渲染,事件這幾部分都詳細看了vue的用法。對比下來,wxml提供的這些特性,跟vue的相關(guān)特性是還比較像,不過功能并沒有那么多,所以也不能輕易地直接拿vue框架的特性用到小程序里面。最佳實踐,還是基于官方文檔中提供的說明來,如果官方文檔中沒有提到的功能,通過猜測的方式去用,肯定是行不通的。我通過打印的方式,查看一些對象的原型,也并沒有發(fā)現(xiàn)比官方文檔要多的一些實例方法,說明小程序的框架功能確實是有限的。

3. wxss其實是可以用less或者sass來寫的,只要選擇器滿足框架的要求即可。由于時間原因,就沒有在這個app里面去嘗試了。

4. 沒有雙向綁定。在vue里面,一個vue實例就是一個view-model;view層對數(shù)據(jù)的更新,會實時反饋到model;model的更新,也會實時反饋的到view。在小程序里面,沒有雙向綁定,view的更新不會直接同步到model;需要在相關(guān)事件回調(diào)里面,直接從view層拿到數(shù)據(jù),然后通過setData的方式,更新model,小程序內(nèi)部會在setData之后重新渲染page。比如單個todo項,toggle的操作:

toggleTodo: function( e ) {

 var id = this.getTodoId( e, 'todo-item-chk-' );
 var value = e.detail.value[ 0 ];
 var complete = !!value;
 var todo = this.getTodo( id );

 todo.complete = complete;
 this.updateData( true );
 this.updateStorage();
},

以上代碼中,通過e.detail.value[0]拿到單個todo項里面checkbox的值,通過該值來判斷todo的complete狀態(tài)。最后在updateData的內(nèi)部,還會通過setData方法,刷新model的內(nèi)容。只有這樣,在toggle操作之后,app底部的統(tǒng)計信息才會更新。

5. 事件綁定的時候,無法傳遞參數(shù),只能傳遞一個event。比如上面那個toggle的操作,我其實很想在回調(diào)里面把當前todo的id傳到這個回調(diào)里面,但是想盡辦法都做不到,最后只能通過id的方式來處理:就是在wxml中綁定事件的組件上面,加一個id,這個id全page也不能重復(fù),所以id得加前綴,然后在id最后加上todo的id值;當事件觸發(fā)的時候,通過e.currentTarget.id就能拿到該組件的id,去掉相應(yīng)的id前綴,就得到todo的id值了。這是目前用到的一個方法,我認為不是很優(yōu)雅,希望后面能發(fā)現(xiàn)更好的辦法來實現(xiàn)。

微信小程序中框架的示例分析

6. app中考慮到了loading的效果,要利用button組件的loading屬性來實現(xiàn)。但是loading僅僅是一個樣式的控制,它不會控制這個按鈕是否能重復(fù)點擊。所以還要利用buttong的disabled屬性,防止重復(fù)點擊。

剩下的實現(xiàn)細節(jié),都在下面兩個文件的源碼中,歡迎大家指出其中的問題。

index.wxml的源碼:

<!--list.wxml-->
<view class="container">
 <view class="app-hd">
  <view class="fx1">
   <input class="new-todo-input" value="{{newTodoText}}" auto-focus bindinput="newTodoTextInput"/> 
  </view>
  <button type="primary" size="mini" bindtap="addOne" loading="{{addOneLoading}}" disabled="{{addOneLoading}}"> 
  + Add
  </button>
 </view>
 <view class="todos-list" >
  <view class="todo-item {{index == 0 ? '' : 'todo-item-not-first'}} {{todo.complete ? 'todo-item-complete' : ''}}" wx:for="{{todos}}" wx:for-item="todo">
   <view wx-if="{{!todo.editing}}">
    <checkbox-group id="todo-item-chk-{{todo.id}}" bindchange="toggleTodo">
     <label class="checkbox">
      <checkbox value="1" checked="{{todo.complete}}"/>
     </label>
    </checkbox-group>
   </view>
   <view id="todo-item-txt-{{todo.id}}" class="todo-text" wx-if="{{!todo.editing}}" bindlongtap="startEdit">
    <text>{{todo.text}}</text>
   </view>
   <view wx-if="{{!todo.editing}}">
    <button id="btn-del-item-{{todo.id}}" bindtap="clearSingle" type="warn" size="mini" loading="{{todo.loading}}" disabled="{{todo.loading}}"> 
     Clear
    </button>
   </view>
   <input id="todo-item-edit-{{todo.id}}" class="todo-text-input" value="{{todo.text}}" auto-focus bindblur="endEditTodo" wx-if="{{todo.editing}}"/> 
  </view>
 </view>
 <view class="app-ft" wx:if="{{todos.length > 0}}">
  <view class="fx1">
   <checkbox-group bindchange="toggleAll">
    <label class="checkbox">
     <checkbox value="1" checked="{{todosOfUncomplted.length == 0}}"/>
    </label>
   </checkbox-group>
   <text>{{todosOfUncomplted.length}} left.</text>
  </view>
  <view wx:if="{{todosOfComplted.length > 0}}">
   <button type="warn" size="mini" bindtap="clearAll" loading="{{clearAllLoading}}" disabled="{{clearAllLoading}}"> 
    Clear {{todosOfComplted.length}} of done.
   </button>
  </view>
 </view>
 <loading hidden="{{loadingHidden}}" bindchange="loadingChange">
  {{loadingText}}
 </loading>
 <toast hidden="{{toastHidden}}" bindchange="toastChange">
  {{toastText}}
 </toast>
</view>

index.js的源碼:

var app = getApp();

Page( {
 data: {
  todos: [],
  todosOfUncomplted: [],
  todosOfComplted: [],
  newTodoText: '',
  addOneLoading: false,
  loadingHidden: true,
  loadingText: '',
  toastHidden: true,
  toastText: '',
  clearAllLoading: false
 },
 updateData: function( resetTodos ) {
  var data = {};
  if( resetTodos ) {
   data.todos = this.data.todos;
  }

  data.todosOfUncomplted = this.data.todos.filter( function( t ) {
   return !t.complete;
  });

  data.todosOfComplted = this.data.todos.filter( function( t ) {
   return t.complete;
  });

  this.setData( data );
 },
 updateStorage: function() {
  var storage = [];
  this.data.todos.forEach( function( t ) {
   storage.push( {
    id: t.id,
    text: t.text,
    complete: t.complete
   })
  });

  wx.setStorageSync( 'todos', storage );
 },
 onLoad: function() {
  this.setData( {
   todos: wx.getStorageSync( 'todos' ) || []
  });
  this.updateData( false );
 },
 getTodo: function( id ) {
  return this.data.todos.filter( function( t ) {
   return id == t.id;
  })[ 0 ];
 },
 getTodoId: function( e, prefix ) {
  return e.currentTarget.id.substring( prefix.length );
 },
 toggleTodo: function( e ) {

  var id = this.getTodoId( e, 'todo-item-chk-' );
  var value = e.detail.value[ 0 ];
  var complete = !!value;
  var todo = this.getTodo( id );

  todo.complete = complete;
  this.updateData( true );
  this.updateStorage();
 },
 toggleAll: function( e ) {
  var value = e.detail.value[ 0 ];
  var complete = !!value;

  this.data.todos.forEach( function( t ) {
   t.complete = complete;
  });

  this.updateData( true );
  this.updateStorage();

 },
 clearTodo: function( id ) {
  var targetIndex;
  this.data.todos.forEach( function( t, i ) {
   if( targetIndex !== undefined ) return;

   if( t.id == id ) {
    targetIndex = i;
   }
  });

  this.data.todos.splice( targetIndex, 1 );
 },
 clearSingle: function( e ) {
  var id = this.getTodoId( e, 'btn-del-item-' );
  var todo = this.getTodo( id );

  todo.loading = true;
  this.updateData( true );

  var that = this;
  setTimeout( function() {
   that.clearTodo( id );
   that.updateData( true );
   that.updateStorage();
  }, 500 );
 },
 clearAll: function() {
  this.setData( {
   clearAllLoading: true
  });

  var that = this;
  setTimeout( function() {
   that.data.todosOfComplted.forEach( function( t ) {
    that.clearTodo( t.id );
   });
   that.setData( {
    clearAllLoading: false
   });
   that.updateData( true );
   that.updateStorage();

   that.setData( {
    toastHidden: false,
    toastText: 'Success'
   });
  }, 500 );

 },
 startEdit: function( e ) {
  var id = this.getTodoId( e, 'todo-item-txt-' );
  var todo = this.getTodo( id );
  todo.editing = true;

  this.updateData( true );
  this.updateStorage();
 },
 newTodoTextInput: function( e ) {
  this.setData( {
   newTodoText: e.detail.value
  });
 },
 endEditTodo: function( e ) {
  var id = this.getTodoId( e, 'todo-item-edit-' );
  var todo = this.getTodo( id );

  todo.editing = false;
  todo.text = e.detail.value;

  this.updateData( true );
  this.updateStorage();
 },
 addOne: function( e ) {
  if( !this.data.newTodoText ) return;

  this.setData( {
   addOneLoading: true
  });

  //open loading
  this.setData( {
   loadingHidden: false,
   loadingText: 'Waiting...'
  });

  var that = this;
  setTimeout( function() {
   //close loading and toggle button loading status
   that.setData( {
    loadingHidden: true,
    addOneLoading: false,
    loadingText: ''
   });

   that.data.todos.push( {
    id: app.getId(),
    text: that.data.newTodoText,
    compelte: false
   });

   that.setData( {
    newTodoText: ''
   });

   that.updateData( true );
   that.updateStorage();
  }, 500 );
 },
 loadingChange: function() {
  this.setData( {
   loadingHidden: true,
   loadingText: ''
  });
 },
 toastChange: function() {
  this.setData( {
   toastHidden: true,
   toastText: ''
  });
 }
});

最后需要補充的是,這個app在有限的時間內(nèi)依據(jù)微信的官方文檔進行開發(fā),所以這里面的實現(xiàn)方式到底是不是合理的,我也不清楚。我也僅僅是通過這個app來了解小程序這個平臺的用法。希望微信官方能夠推出一些更全面、最好是項目性的demo,在代碼層面,給我們這些開發(fā)者提供一個最佳實踐規(guī)范。

感謝各位的閱讀!關(guān)于“微信小程序中框架的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI