午夜91福利视频,午夜成人在线观看,午夜在线视频免费观看,午夜福利短视频,精品午夜成人免费视频APP

小程序模板網

微信小程序組件的引入和調用的方法

發布時間:2021-06-24 08:50 所屬欄目:小程序開發教程

看一下(xia)我的(de)組(zu)件結構 components(被調(diao)用(yong)的(de)組(zu)件) index(頁面組(zu)件)請忽略調(diao)圖片

 module文件就是我所創的自定義組件,module.wxml文件代碼為:
 

<view class="inner">
  <Text>I am learning 微信小程序</Text>
</view>

  module.js文(wen)件代碼為:

const app = getApp()
Component({
  properties: {
    // 這里定義了innerText屬性,屬性值可以在組件使用時指定
    innerText: {
      type: String,
      // value: '',
    }
  },
  data: {
    // 這里是一些組件內部數據
    someData: {}
  },
  methods: {
    customMethod(){
      console.log('hello world! I am learning 微信小程序')
    }
  }
})

現在我要在pages/index文件(jian)中(zhong)使用(yong)該(gai)自定義組(zu)件(jian),該(gai)怎么做呢?

1、在module.json文件中添加以下(xia)代碼:

{
  "component": true
}

2、在需要調用自定義組件的文件中,如pages/index文件需要調用自定義組件,那(nei)么則需要在pages/index/index.json文件中添(tian)加如下代碼:

{
  "usingComponents": {
    "module": "../../components/module/module"   // 組件名以及組件所在路徑
  }
}

3、然后就(jiu)可(ke)以在(zai)module.wxml文件(jian)中使用(yong)該自定義組件(jian)了,

  index.wxml文件代碼如下:

<view>
    <module id="module"></module>
</view>

此時調用(yong)自定義組(zu)件(jian)后,效果(guo)如下:

4、現(xian)在要調用自定義組件(jian)中(zhong)的(de)(de)方法就可以這樣(yang)做(zuo),為了方便,這里我使用的(de)(de)是點擊(ji)按鈕(niu)調用事(shi)件(jian),因此index.wxml文件(jian)代碼變為:

<view>
    <button bindtap="showComponent">組件調用</button>
    <module id="module"></module>
</view>

5、index.js文件部分代碼為:
 

const app = getApp()
import { Resume } from '../../request/api'
const request = new Resume()
Page({
  data: {
     
  },
  onLoad: function () {
    // request.testInitial({ 'name':'123' }).then(res=>{
    //   console.log(res)
    // })
  },
  onReady: function () {
    //  頁面初次渲染完成后,使用選擇器選擇組件實例節點,返回匹配到組件實例對象 
    this.module= this.selectComponent('#module')
  },
  showComponent: function () {
    let module= this.module
    module.customMethod()  // 調用自定義組件中的方法
 }
})

  最后的結果:


易優小(xiao)程序(企業版)+靈(ling)活api+前(qian)后代碼開(kai)源 碼云倉庫:
本文地址://www.jinyoudianli.com/wxmini/doc/course/26720.html 復制鏈接 如需定制請聯(lian)系易優客(ke)服咨詢(xun):

工作日 8:30-12:00 14:30-18:00
周六及部分節(jie)假(jia)日(ri)提供值班服務

易小優
轉(zhuan)人工 ×