目录

Component Style and Isolation Strategy

The CSS file of the component takes effect only for nodes in the HXML file of the component. When compiling the component style, pay attention to the following points:

Component Style Isolation

Generally, the style of a custom component is subject only to the CSS file of the custom component. You can also specify styleIsolation to isolate different styles. You can configure styleIsolation in options of the JS file.

Component({
  options: {
    styleIsolation: 'isolated'
  }
})

The styleIsolation option supports the following values:

You can also set addGlobalClass: true in the options of the Component. This option is equivalent to styleIsolation: apply-shared. However, after styleIsolation is set, this option becomes invalid.

Component({
  options: {
    addGlobalClass: true
  }
})

Since ASCF runtime version 1.0.15 and ASCF Toolkit version 1.0.10, the styleIsolation option in the JSON file of the custom component is supported. The priority of the styleIsolation option in the JSON file is higher than that in the JS file.

{
  "styleIsolation": "isolated"
}

External Style Classes

To enable a component to receive external style classes, you can use the externalClasses field in Component to define multiple external style classes.

This feature can be used to implement a property similar to the hover-class property of the view component. Specifically, a page can provide a style class to assign the hover-class property of the view component to the page. The style class is implemented on the page instead of the view component.

Example:

// Component custom-component.js
Component({
  externalClasses: ['my-class']
});
<!-- Component my-component.hxml -->
<view class="my-class">This text style is determined by the class outside the component.</view>

After the component is defined, you can specify the class corresponding to the style class, which can be used in the same way as common properties.

Example:

<!-- Page HXML -->
<my-component my-class="red-text" />
<my-component my-class="large-text" />
<my-component my-class="red-text large-text" />
.red-text {
    color: red;
}
.large-text {
    font-size: 1.5em;
}