第一步,在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的值的
就是這么簡單粗暴的不行