tencent cloud

Feedback

Referencing

Last updated: 2024-11-21 18:34:18
    WXML provides two referencing methods import and include.

    import

    import can use the template defined by the target file in the current file, for example:
    A template named item is defined in item.wxml:
    <!-- item.wxml -->
    <template name="item">
    <text>{{text}}</text>
    </template>
    By referencing item.wxml in index.wxml, you can use the item template:
    <import src="item.wxml"/>
    <template is="item" data="{{text: 'forbar'}}"/>

    import Scope

    The concept of “scope” is used for import references. This means that the reference will import the template defined in the target file, but will not import any import template in the target file.
    For example: In the case C import B, B import A, the template defined by B can be used in C and the template defined by A can be used in B. However, C cannot use the template defined by A.
    <!-- A.wxml -->
    <template name="A">
    <text> A template </text>
    </template>
    <!-- B.wxml -->
    <import src="a.wxml"/>
    <template name="B">
    <text> B template </text>
    </template>
    <!-- C.wxml -->
    <import src="b.wxml"/>
    <template is="A"/> <!-- Error! Can not use tempalte when not import A. -->
    <template is="B"/>

    include

    include can introduce the entire code excluding <template/> <wxs/>. This is equivalent to copying the code to the location of include. For example:
    <!-- index.wxml -->
    <include src="header.wxml"/>
    <view> body </view>
    <include src="footer.wxml"/>
    <!-- header.wxml -->
    <view> header </view>
    <!-- footer.wxml -->
    <view> footer </view>
    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