Hello everyone, I am V. In the development of Hongmeng NEXT@Styles
Decorators are a very useful method for defining reusable styles. This allows developers to distill multiple style settings into a single method that can be reused across multiple components, thus improving code maintainability and readability. The following@Styles
Detailed usage and application scenario examples of decorators.
@Styles
Instructions for using the decorator
-
Only generic properties and events are supported: Current
@Styles
Applies only to generic properties and events; properties or events specific to a component are not supported. -
Parameters not supported:
@Styles
Methods cannot take parameters. For example, the following is an example of incorrect usage:
@Styles function globalStyles(value: number) {
.width(value)
}
The correct way to do this is without parameters:
@Styles function globalStyles() {
.width(150)
.height(100)
.backgroundColor()
}
-
Define Position:
@Styles
It can be defined within a component or globally. When defined globally, the method name needs to be preceded by thefunction
keyword; when defined within a component, it is not required.
// global definition
@Styles function globalStyles() {
.width(150)
.height(100)
.backgroundColor()
}
// In-component definition
@Component
struct FancyComponent {
@Styles myStyle() {
.width(200)
.height(100)
.backgroundColor()
}
}
-
Accessing the internal state of a component: Defined within the component
@Styles
This can be done bythis
access to the component's constants and state variables, and can be used in the@Styles
The value of a state variable is changed by an event in the state variable.
@Component
struct MyComponent {
@State heightValue: number = 100
@Styles myStyle() {
.height()
.backgroundColor()
.onClick(() => {
= 200
})
}
}
-
prioritization: Within the component
@Styles
has a higher priority than the global@Styles
. The framework prioritizes finding the current component within the@Styles
, if it is not found, it will be looked up globally.
Let's look at a case study.
The following is an example of how to use the@Styles
Decorator example showing how to define styles globally and within a component, and use those styles within the component.
// Defining global styles
@Styles function globalStyles() {
.width(150)
.height(100)
.backgroundColor()
}
@Entry
@Component
struct StyleUse {
@State heightValue: number = 100
// Defining In-Component Styles
@Styles myStyle() {
.width(200)
.height()
.backgroundColor()
.onClick(() => {
= 200
})
}
build() {
Column({ space: 10 }) {
// Using Global Styles
Text('FancyA').globalStyles().fontSize(30)
// Using In-Component Styles
Text('FancyB').myStyle().fontSize(30)
}
}
}
In this case, we define a global styleglobalStyles
and an in-component stylemyStyle
The InStyleUse
integrated componentbuild
method, we use these styles to set the twoText
component's style. This shows how to style a component with the@Styles
Decorators reuse styles , making the code more concise and easy to maintain .
How can I use @Styles decorator to optimize my component code?
utilization@Styles
Decorators can significantly optimize your component code, making it cleaner, more maintainable, and reducing duplicate style code. Here's how to use@Styles
Steps and examples of decorators to optimize component code:
1. Recognize duplicate style codes
First, you need to identify style codes that are reused across multiple components. These could be generic layout styles, colors, font sizes, etc.
2. Definitions@Styles
methodologies
Distill these duplicate style codes into one or more@Styles
methods. You can define these methods inside the component or globally.
-
In-Component Styling: If the style is only used in a single component, it can be defined inside that component.
@Styles
Methods. -
Global Style: If the style is used in more than one component, it can be defined as a global
@Styles
Methods.
3. Applications@Styles
methodologies
In the component'sbuild
method, applying these by means of method calls in the@Styles
method to the appropriate UI component.
4. Use of status and events
If desired, you can add the@Styles
method uses the component's state and event handlers so that the style can change dynamically based on the component's state.
Optimization example
Let's say you have an application where multiple pages have warning messages that need to be displayedText
component, the style of the warning message is consistent across all pages: red, bold, and font size 16.
Step 1: Define Global@Styles
methodologies
// Define global warning styles
@Styles function warningStyle() {
.fontSize(16)
.fontWeight()
.fontColor()
}
Step 2: Use the@Styles
methodologies
@Entry
@Component
struct WarningMessage {
build() {
Column() {
// Use the global warning style
Text('This is a warning message').warningStyle()
}
}
}
In this example, we define a global warning stylewarningStyle
and inWarningMessage
The style is applied in the component. This way, no matter what part of the application needs to display the warning message, you can simply call the.warningStyle()
method to apply the style without having to rewrite the style code.
summarize
By using the@Styles
decorators, you can separate the style logic from the component's business logic, making the code clearer and easier to manage. This not only reduces code duplication, but also makes style modification and maintenance more centralized and efficient. The light boat has crossed ten thousand mountains, the momentum of Hongmeng is unstoppable. Concerned about Wei brother love programming, together mixed into the Hongmeng ecological.