All Products
Search
Document Center

Mobile Platform as a Service:Component template and style

Last Updated:Nov 24, 2022

Similar to pages, custom components can have their own AXML templates and ACSS styles.

Similar to pages, custom components can have their own AXML templates and ACSS styles.

AXML

AXML is a mandatory part of a custom component.

<!-- /components/index/index.axml -->
<view onTap="onMyClick" id="c-{{$id}}"/>
// /components/index/index.js
Component({
  methods: {
      onMyClick(e) {
      console.log(this.is, this.$id);
    },
  },
});

Note: Unlike pages, custom events need to be placed in methods.

Slot

If the component js supports props, a custom component can interact with external callers and receives data from external callers. It can also call functions from external callers and notifies external callers of changes in the component.

However, custom components are not flexible enough. In addition to data processing and notification, the slot provided by the Mini Program can be used to assemble the axml structure of a custom component and the axml passed from an external caller. An external caller passes axml to the custom component, and the custom component uses it to assemble the final component axml structure.

Default slot

Sample code:

<!-- /components/index/index.axml -->
<view>
  <slot>
    <view>default slot & default value</view>
  </slot>
  <view>other</view>
</view>

Caller does not pass AXML:

// /pages/index/index.json
{
   "usingComponents": {
     "my-component": "/components/index/index"
   }
}
<!-- /pages/index/index.axml -->
<my-component />

Output:

default slot & default value
other

Caller passes axml:

<!-- /pages/index/index.axml -->
<my-component>
  <view>header</view>
  <view>footer</view>
</my-component>

Output:

header
footer
other

If the caller does not pass axml between the component tags <xx>, the default slot is rendered. If the caller passes axml between the component tags <xx>, axml is used instead of the default slot to assemble the final amxl for render.

Named slot

The default slot can only pass one copy of axml. A complex component needs to render different axmls in different positions, that is, multiple axmls need to be passed. Therefore, the named slot is required. If the named slot is used, the external caller can specify a part of axml in the subtag of the custom component tag and put it into a specific named slot of the custom component. The other part in the sub-tab of the custom component tag that does not have a specified named slot will be placed in the default slot. If the named slot is passed only, the default slot will not be overwritten.

Sample code:

<!-- /components/index/index.axml -->
<view>
  <slot>
    <view>default slot & default value</view>
  </slot>
  <slot name="header"/>
  <view>body</view>
  <slot name="footer"/>
</view>

Passes the named slot only:

<!-- /pages/index/index.axml -->
<my-component>
  <view slot="header">header</view>
  <view slot="footer">footer</view>
</my-component>

Output:

default slot & default value
header
body
footer

Passes the named slot and the default slot:

<!-- /pages/index/index.axml -->
<my-component>
  <view>this is to default slot</view>
  <view slot="header">header</view>
  <view slot="footer">footer</view>
</my-component>

Output:

this is to default slot
header
body
footer

slot-scope

When the named slot is used, the custom component either uses its own axml or axml of an external caller (such as a page).If axml of the custom component is used, the internal component data can be accessed. With the help of props, the data of the external caller can also be accessed.

Sample code:

// /components/index/index.js
Component({
  data: {
    x: 1,
  },
  props: {
    y: '',
  },
});
<!-- /components/index/index.axml -->
<view>component data: {{x}}</view>
<view>page data: {{y}}</view>
// /pages/index/index.js
Page({
  data: { y: 2 },
});
<!-- /pages/index/index.axml -->
<my-component y="{{y}}" />

Output on the page:

component data: 1
page data: 2

If the custom component uses axml of an external caller (such as a page), only the external caller can be accessed.

Sample code:

<!-- /components/index/index.axml -->
<view>
  <slot>
    <view>default slot & default value</view>
  </slot>
  <view>body</view>
</view>
// /pages/index/index.js
Page({
  data: { y: 2 },
});
<!-- /pages/index/index.axml -->
<my-component>
  <view>page data: {{y}}</view>
</my-component>

Output:

page data: 2
body

The slot scope allows the content of the slot to access the internal component data.

Sample code:

// /components/index/index.js
Component({
  data: {
    x: 1,
  },
});
<!-- /components/index/index.axml -->
<view>
  <slot x="{{x}}">
    <view>default slot & default value</view>
  </slot>
  <view>body</view>
</view>
// /pages/index/index.js
Page({
  data: { y: 2 },
});
<!-- /pages/index/index.axml -->
<my-component>
  <view slot-scope="props">
    <view>component data: {{props.x}}</view>
    <view>page data: {{y}}</view>
  </view>
</my-component>

Output:

component data: 1
page data: 2
body

As shown above, the custom component defines the slot property to expose the internal component data. When the page uses the component, the scope slot is used, and the property value is defined using the temporary variable name props. In this way, the internal data of the component can be accessed.

acss

Like pages, custom components can define their own acss styles. acss will be automatically introduced into the page that uses the component. For details, see acss syntax.