1. 程式人生 > >微信小程式 template新增繫結事件

微信小程式 template新增繫結事件

對於模板的使用,我是想將模板的事件單獨出來,其他引用模板的頁面中不再摻雜模板事件,比較方便管理,如果還有其他好的解決辦法, 請賜教。

template.wxml

<view bindtap="clickView" class="tempClass">temp模板</view>

template.js

var temp = {
  clickView: function () {
    console.log("剛剛您點選了temp")
  }
}
export default temp

template.wxss

.tempClass {
  background-color
: red
; }

————————————————————————
index.wxml 引用template模板

<!--index.wxml-->
<view>body</view>
<include src="../temp/temp.wxml" />

index.js

const App = getApp()
import tempObj from '../temp/temp'

//index.js
//獲取應用例項
var indexObj = {
  onShow() {
    console.log("onShow")
  }
}
indexObj["clickView"
] = tempObj.clickView Page(indexObj)

index.wxss

/**index.wxss**/
@import "../temp/temp.wxss";