微信小程序学习目标

110 阅读2分钟

WeChat5fb6422cfa62690061cc8dc0750d7dce.png

自定义小程序组件

WeChat6038223277939538512c73ab031e5721.png

WeChat9841586b9b36eed2c8d7252d00f1b6a4.png

局部引用在页面的json文件中引用的方式叫局部引用.

{
	{
	"usingComponents": {
		"test1":"/components/test/test"
	},
	"enablePullDownRefresh": true,
	"backgroundColor": "#ffffff",
	"backgroundTextStyle": "dark"
	
}
	
}
//使用
<test1></test1>

在app.json中引用组件就是全局引用

        "usingComponents": {
		"test":"/components/test/test"
	}
        

WeChat6ec17e7533c18ba63e43c7a0c8914b8e.png

WeChat2c4b75981a1aaa76768e035455a526c2.png

WeChat33242d5160b07dee631a3a5d25c8daec.png

WeChat5c6a8dc075c7d577539af8b37d492ffc.png

WeChatb677b444c284dcc7724558046eb7220e.png

	properties: {
		// 对外暴露的属性
		max:{
			type: Number,
			value: 10
		}
	},

        methods: {
		addCount() {
			if (this.data.count >= this.properties.max) {
				return
			}
			this.setData({count: this.data.count + 1})
			wx.showToast({
			  title: '数字' + this.data.count,
			})
		}
	}

WeChat4f9820a1f8d8e46071715ded342568d3.png

WeChat22aaa2b62b680789b90ece2b2f9393fa.png

<view>
	{{n1}} + {{n2}} = {{sum}}
</view>

<button bindtap="addN1">n1 + 1</button>

<button bindtap="addN2">n2 + 1</button>
	observers:{
		// 监听数据节点
		'n1, n2': function (n1, n2) {
			// 监听n1,n2的数据变化
			this.setData({sum: n1 + n2})
			
		}
	}
        
        
	methods: {
		addN1() {this.setData({n1:this.data.n1 + 1})},
		addN2() {this.setData({n2:this.data.n2 + 1})},
        }
        
	data: {
		count: 0,
		n1:0,
		n2:0, 
		sum:0
	},        
        
        

WeChat4499bf61d9da32aea8d5f251ef46843a.png

data: {
		count: 0,
		n1:0,
		n2:0, 
		sum:0,
		rgb:{
			r:0,
			g:0,
			b:0
		},
		fullColor: '0,0,0'
	},
	methods: {
		addN1() {this.setData({n1:this.data.n1 + 1})},
		addN2() {this.setData({n2:this.data.n2 + 1})},

		changeR(){
			this.setData({'rgb.r':this.data.rgb.r + 5 > 255 ? 255:this.data.rgb.r+5})
		},
		changeG(){
			this.setData({'rgb.g':this.data.rgb.g + 5 > 255 ? 255:this.data.rgb.g+5})

		},
		changeB(){
			this.setData({'rgb.b':this.data.rgb.b + 5 > 255 ? 255:this.data.rgb.b+5})

		},
observers:{
		// 监听数据节点
		'n1, n2': function (n1, n2) {
			// 监听n1,n2的数据变化
			this.setData({sum: n1 + n2})
			
		},
		//监听对象
		'rgb.r, rgb.g, rgb.b': function (r, g, b) {
			this.setData({fullColor:`${r},${g},${b}`})
			 
		}
                //可以换种写法
                'rgb.**': function (rgb) {
			this.setData({fullColor:`${rgb.r},${rgb.g},${rgb.b}`})
			 
		}

	}

WeChatf0245c4944ff09e326ad812de25b5095.png

WeChat4bfb4231bec521ecb2d88e5908a990d1.png

WeChat3d0bcb8a77d6ffe9921cbf816cca086f.png

WeChatd6889374ee65e01676a72918fae66f13.png

lifetimes:{
		attached(){},
		detached(){}
	},

WeChat281d7c1b632441dd08a9177f4efd93e9.png

// 在组件中监听页面的生命周期函数
	pageLifetimes:{
		// 页面被展示
		show:function(){},
		// 页面被隐藏
		hide:function(){},
		// 页面尺寸变化
		resize:function(){}
	},

WeChata827b8fd3b5dfb52361fca0d92e9f026.png

	options:{
		// 启用多个插槽
		multipleSlots: true
	},
<view class="colorBox">
	<!-- 单个插槽不需要name来区分 -->
	<slot name="slot1"></slot>
	<slot name="slot2"></slot>
</view>


//使用
<test2>
	<text slot="slot1">张凯强</text>
	<!-- <text slot="slot2">徐 蓉彬</text> -->

</test2>

WeChatfea27c94361de21d398ae579840cb212.png

<!-- 父组件通过属性向子组件传值 -->
<test2 count="10">

</test2>


properties: {
		count:Number
	},

子组件向父组件传值的话。需要事件绑定

WeChatdd7b0233ca152f06dfaedef700df80d0.png

// 第一步在父组件的js文件中自定义一个函数,这个函数将通过自定义时间的形式传递给子组件
	syncCount() {
		// 第4步,在父组件内部通过e.detail接受数据
		console.log(e.detail)
		this.setData({
			count: e.detail.value
		}) 
	}
        <!-- 第二步在父组件的wxml文件中通过自定义时间的形式。讲步骤一中的函数引用传递给子组件 -->
<test2 count="10" bind:sync="syncCount">

</test2>


methods: {
		// 第三步,在子组件内部js中通过调用以下方法将数据发送到父组件
		addCount() {
			this.setData({
				count: this.properties.count + 1
			})
			this.triggerEvent('sync', {value:this.properties.count})
		}
	}

<!-- 自定义组件,父子之间的通信 -->
<!-- 获取组件的实例对象,需要给子组件一个类选择器或者是id选择器 -->
<test2 count="{{100}}" bind:sync="syncCount" class="custom1" id="c1"></test2>

<button bindtap="getChild">获取子组件实例</button>



getChild(e){
		// 通过类选择器或者是id选择器获取子组件的实例
		// const child = this.selectComponent(".custom1")
		const child = this.selectComponent("#c1")
		// 调用子组件的set方法
		child.setData({count:child.properties.count + 1})
		// 调用子组件的addCount方法
		child.addCount()
	}

知道小程序组件中behaviors的作用

behaviors 是实现组件间代码共享的特性。

WeChatf2734970fcaa467e9eea0e640090072c.png

behaviors是包含一组属性,数据,生命周期函数和方法。组件引用的时候。他的属性数据和方法会被合并到组件中.每个组件可以引用多个behavior。同时behavior也可以引用其他behavior

module.exports = Behavior({
	//私有数据节点
	data:{

	},
	// 私有数据节点
	properties:{},
	// 事件处理函数,自定义方法的节点
	methods:{}
})

//在组件中使用require方法导入需要的behavior
const myBehavior = require("../../behaviors/my-behavior")


Component({


	// 将导入的behavior对象挂载到behaviors数组节点中即可生效
	behaviors:[
		myBehavior
	],

WeChatf582ca52ea6781873feae38eda974c92.png behavior详细介绍