自定义组件-微信小程序

自定义组件的创建方法

  • 在根目录下创建components文件夹(为了区分一般的文件夹)
  • 在components下面创建新的组件名文件夹,在新创建的组件名文件夹下继续创建component
  • 在创建好的组件的xhtml中写出想要显示的内容,在wxss中编辑样式
  • 比如是在主页index中想要调用自定义组件,那么就需要在index.json中进行配置
    (属性名是组件名,值就是组件的路径)
    例如:
"usingComponents": {
    "xsj":"/components/xsj/xsj",
    "HdView":"/components/HdView/HdView",
    "FoView":"/components/FoView/FoView",
    "PubTitle":"/components/PubTitle/PubTitle"
  },

在index.wxml中就可以使用自定义的组件了:
有两种方法:

<xsj></xsj>
<xsj/>

自定义组件传递属性

  • 在自定义组件还是那个传递自定义属性
 <PubTitle myTitle="新闻" myUrl="/pages/ps/ps"/>
  • 需要来PubTitle自定义组件.js中接收该自定义属性,如:
properties: {

    myTitle:{
      type:String,/*值的类型 */
      value:""   /*值 */

    },
    myUrl:{
      type:String,
      value:""
    }

  },
  • 在PubTitle.wxml中渲染从前端传递过来的属性:
<view class="pubTitle">
    <view class="txt">{
   
   {myTitle}}</view>
    <navigator open-type="reLaunch" class="more" url="{
     
     {myUrl}}">更多>></navigator>
  </view>

猜你喜欢

转载自blog.csdn.net/m0_47484802/article/details/115417338