小程序 自定義組件 子組件傳父組件 父組件傳子組件

場景:多個頁面使用 UI畫面基本一致

舉個栗子:拿多個頁面 都有搜索框 點擊跳搜索頁面

實施:

? ? ? ? 創(chuàng)建公用組件頁面?search

? ??????search.wxml? ??

????????<view class="search">

? ????????<view class="search-inputView flex-align-center" bindtap=" callFather ">

? ? ????????<image src="/assets/icons/icon_fd.png" bindtap=" jumpsearch "></image>

? ? ????????<input placeholder="{{inputValue}}" value="" disabled></input>

? ????????</view>

????????</view>

? ??????search.css

????????.search {position: fixed;top: 0;z-index: 1001;width: 100%; background: #fff; left: 0;font-size: 28rpx;}

????????.flex-align-center{display: flex;align-items: center}

????????.search-inputView { width: 92%;box-sizing: border-box; margin: 20rpx 0;margin-left: 4%; height: 70rpx;line-height: 70rpx;

? ????????????background: rgba(245, 247, 252, 1);padding: 0 30rpx; border-radius: 45rpx;}

????????.search-inputView input {width: 85%;margin-left: 10rpx; margin-right: 20rpx;}

????????.search-inputView image {width: 35rpx;height: 35rpx;}

????????.scroll-view {position: fixed;top: 180rpx;left: 0;z-index: 10002;}

? ??????search.js

Component({

? properties: {

? ? inputValue: {

? ? ? type: String,

? ? ? value: 'default value',

? ? }

? },

? data: {

? ? // 這里是一些組件內(nèi)部數(shù)據(jù)

? ? someData: {}

? },

? created() {

? ? console.log('被使用了~~~')

? },

? methods: {

? ? jumpsearch: function() {

? ? ? wx.navigateTo({

? ? ? ? url: '/pages/search/search'

? ? ? })

? ? },

? ? // 子組件點擊 傳遞參數(shù)到父組件 調(diào)用父組件事件?

? ? callFather: function (e) {

? ? ? this.triggerEvent('callFather', e ) // callFather 自定義名稱事件,父組件中接收

? ? },

? }

})

? ??????



? ? ? ?使用組件頁面

? ? ? ? index.wxml

? ??????<search inputValue="{{placeholderValue}}"? bind: callFather =" callFather "></search>

?????????index.json

? ??????{

????????????"navigationBarTitleText": "首頁",

? ? ? ? ? ?"usingComponents": {

? ? ? ? ? ????????"search": "../public/search",

? ????????????}

????????}

? ??????index.js

? ??????Page({

? ????????data: {placeholderValue:'哈哈哈'},

? ? ? ? ? ?callFather (e){

? ? ? ? ? ? console.log(e) //父組件接收 子組件傳遞的參數(shù)

????????????}????

????????})

? ? ? ? ? ? 注釋:公用組件的css 是獨立的? 沒法調(diào)用到app.css 的class

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容