(一)本節主要是講述利用微信小程序完成組件功能
(1) 首先在要使用組件的頁面里面加入代碼。必須在.json文件里面加入
(2) 名字可以隨便寫。但是要注意路徑問題。最開始不加/表示相對路徑。加了/表示絕對路徑
{
"usingComponents": {
"s-like":"/components/like/index"
}
}
(二) 之列要注意的是小程序綁定數據和vue不一樣。他沒有:全靠雙引號和bind
(三)開始寫組件,這里寫組件的方法和vue類似。也需要發射出去
// components/like/index.js
Component({
/**
* 組件的屬性列表
*/
properties: {
//接收一個數值
count:Number,
//接受一個flag用來切換圖標
like:{type:Boolean}
},
/**
* 組件的初始數據
*/
data: {
img1:'images/like.png',
img2:'images/like@dis.png'
},
/**
* 組件的方法列表
*/
methods: {
change(){
let flag = this.properties.like;
let count = this.properties.count;
let result_flag = ! this.properties.like;
console.log(count);
let result_count = result_flag?++count:--count
this.triggerEvent("likechange", {likeflag:result_flag,likecount:result_count})
}
}
})