微信小程序创建一个组件

创建一个微信小程序组件

官方教程:自定义组件

创建组件:

  1. 在项目根目录创建一个components文件夹

  2. 在components下创建一个组件的文件夹,用组件名来命名

  3. 鼠标右击该文件夹创建这个组件
    微信小程序创建一个组件_第1张图片

使用组件:

  1. 在需要使用组件的页面中注册组件,修改index.json文件,将组件添加到usingComponents项中

    {
      "usingComponents": {
          "Header":"/components/Header/Header"
      }
    }
    
  2. 在index.wxml中使用

    <view class="container">
        <Header id="header">Header>
    view>
    
  3. 获取组件

    this.selectComponent('#header')
    

问题:

  1. 在组件中无法应用全局样式,导致不能使用字体图标。

    解决办法:修改组件中的js文件。

    // components/Header/Header.js
    Component({
        properties: {},
        data: {},
        methods: {},
    
        /*加上这一项,使用全局样式 */
        options: {
            addGlobalClass: true
        },
    })
    

你可能感兴趣的:(微信小程序)