微信小程序組件封裝


第一步,在page下面新建一個template文件,如下圖

第二部,在template.wxml中編寫公用組件即要封裝的代碼模塊

<!--pages/template/template.wxml-->
<template name="mars">
  <view>
    <text>mars{{step}}</text>
  </view>
</template>

第三部,在需要使用封裝的組件的wxml文件里引入,此步最關鍵

<!--pages/index/index.wxml-->
<view class='address' bindtap='onChangeAddress'>
  <template is="mars" data="{{selectRole:'A',step:0}}"></template>
  <input class='choose-address' placeholder='請選取地點' value='{{chooseAddress}}'></input>
</view>
<import src="/pages/template/template.wxml" />

注意:組件的傳值我也卸載代碼中了,在父組件index.wxml中添加 data="{{selectRole:'A',step:0}}" 在子組件中是可以直接獲取到selectRole和step的值的

就是這么簡單粗暴的不行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM