tencent cloud

Feedback

Custom module components

Last updated: 2024-03-04 23:14:48
    Within the realm of Tencent Cloud's Mini Program development platform, developers are empowered to abstract functional modules within pages into custom components, thereby facilitating their repeated use across diverse pages. Concurrently, intricate pages can be deconstructed into multiple low-coupling modules, thereby contributing significantly to code maintenance and reuse.

    Create custom components

    A custom component is constituted by four files: json, wxml, wxss, and js. To craft such a component, an initial step involves declaring the custom component within the json file, setting the 'component' field to 'true'. This serves to inform developers that this file set constitutes a custom component.
    {
    "component": true
    }
    Simultaneously, it is requisite to script the component template within the wxml file and incorporate component styles within the wxss file. Their scripting style bears resemblance to that of page scripting. For specific details and cautionary notes, see Component Templates and Styles.
    Sample code:
    <!-- This represents the internal WXML structure of the custom component -->
    <view class="inner">
    {{innerText}}
    </view>
    <slot></slot>
    /* The styles here are solely applicable to this custom component */
    .inner {
    color: red;
    }
    Note:
    In the component wxss, the use of ID selectors, attribute selectors, and tag name selectors should be avoided.
    Within the js file of the custom component, it is necessary to register the component using Component(), and provide the component's attribute definitions, internal data, and custom methods.
    The component's attribute values and internal data are used for the rendering of the component's wxml, wherein, the attribute values can be passed in from outside the component. For more details, see the Component Constructor.
    Sample code:
    Component({
    properties: {
    // Here, the innerText attribute is defined, the value of which can be specified when the component is used.
    innerText: {
    type: String,
    value: 'default value',
    }
    },
    data: {
    // Here are some internal data of the component.
    someData: {}
    },
    methods: {
    // Here is a custom method.
    customMethod() {}
    }
    })

    Using custom components

    Before using a registered custom component, it is necessary to first declare a reference in the page's json file. At this point, it is required to provide the tag name and corresponding file path for each custom component:
    {
    "usingComponents": {
    "component-tag-name": "path/to/the/custom/component"
    }
    }
    In this manner, within the page's wxml, the custom component can be used just as a basic component would be. The node name corresponds to the tag name of the custom component, while the node attributes represent the attribute values passed to the component.
    Note:
    Declare the usingComponents field in app.json. The custom components declared here are considered global custom components, which can be directly used in the pages or custom components of the mini program without further declaration.
    <view>
    <!-- Below is a reference to a custom component -->
    <component-tag-name inner-text="Some text"></component-tag-name>
    </view>
    After being combined with the data, the wxml node structure of the custom component will be inserted into the reference location.

    Points of Detail to Note

    Details to be Mindful of:
    The tag names of custom components and WXML node tag names only support combinations of lowercase letters, hyphens, and underscores.
    Custom components can also reference other custom components, with the method of reference similar to how pages reference custom components (using the usingComponents field).
    The root directory names of custom components and pages cannot begin with the prefix "wx-". Otherwise, an error will be reported.
    The use of usingComponents in page files may result in slight differences in the prototype of the page's this object, including:
    The prototype of a page using usingComponents is inconsistent with that of a page not using it, meaning the result of Object.getPrototypeOf(this) will differ.
    When using usingComponents, additional methods such as selectComponent will be available.
    For performance reasons, when using usingComponents, the content of setData will not be directly deep-copied, meaning this.setData({ field: obj }) will result in this.data.field === obj. (Deep copying will occur when this value is passed between components.)
    Note:
    If the page is complex, it is recommended to retest when adding or deleting the usingComponents definition section.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support