已合并
styles extend支持if else语句 #9726
styles extend支持if else语句 #9726
已合并
BlYynNe创建于 6月2日
9 个文件变更+117-1
@@ -40,7 +40,8 @@ entry/src/main/ets/
40 ├── Index.ets40 ├── Index.ets
41 ├── StylesDecorator41 ├── StylesDecorator
42 │   ├── StylesDecorator1.ets // 全局@Styles的用法42 │   ├── StylesDecorator1.ets // 全局@Styles的用法
43- │   ── StylesDecorator2.ets // 组件内的@Styles用法43+ │   ── StylesDecorator2.ets // 组件内的@Styles用法
44+ │   └── StylesDecorator3.ets // @Styles函数和方法支持if else
44 └── common45 └── common
45 └── Index.ets46 └── Index.ets
46entry/src/ohosTest/47entry/src/ohosTest/
@@ -57,6 +57,7 @@ struct ComponentExtension {
57 children: [57 children: [
58 { name: 'StylesDecorator1', label: $r('app.string.StylesDecorator1') },58 { name: 'StylesDecorator1', label: $r('app.string.StylesDecorator1') },
59 { name: 'StylesDecorator2', label: $r('app.string.StylesDecorator2') },59 { name: 'StylesDecorator2', label: $r('app.string.StylesDecorator2') },
60+ { name: 'StylesDecorator3', label: $r('app.string.StylesDecorator3') },
60 ]61 ]
61 },62 },
62 ];63 ];
@@ -0,0 +1,54 @@
1+/*
2+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License")
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+import deviceInfo from '@ohos.deviceInfo';
17+ 
18+// 在@Styles函数中使用if else实现不同版本之间的属性差异
19+@Styles
20+function someStyles() {
21+ // 条件表达式中支持使用deviceInfo命名空间中获取版本的属性或者方法进行判断
22+ if (deviceInfo.sdkApiVersion >= 26) {
23+ .backgroundColor(Color.Red)
24+ } else {
25+ .backgroundColor(Color.Black)
26+ }
27+}
28+ 
29+@Entry
30+@Component
31+struct Index {
32+ @State message: string = 'Hello World';
33+ 
34+ // 局部@Styles方法同样支持使用if else实现不同版本之间的属性差异
35+ @Styles
36+ someOtherStyles() {
37+ if (deviceInfo.sdkApiVersion < 24) {
38+ .width(200)
39+ } else {
40+ .width(300)
41+ }
42+ }
43+ 
44+ build() {
45+ Column() {
46+ Text('1')
47+ .someStyles()
48+ Text('2')
49+ .someOtherStyles()
50+ }
51+ .height('100%')
52+ .width('100%')
53+ }
54+}
@@ -35,6 +35,10 @@
35 {35 {
36 "name": "StylesDecorator2",36 "name": "StylesDecorator2",
37 "value": "StylesDecoratorTwo"37 "value": "StylesDecoratorTwo"
38+ },
39+ {
40+ "name": "StylesDecorator3",
41+ "value": "StylesDecoratorThree"
38 }42 }
39 ]43 ]
40}44}
@@ -3,6 +3,7 @@
3 "pages/Index",3 "pages/Index",
4 "pages/StylesDecorator/StylesDecorator1",4 "pages/StylesDecorator/StylesDecorator1",
5 "pages/StylesDecorator/StylesDecorator2",5 "pages/StylesDecorator/StylesDecorator2",
6+ "pages/StylesDecorator/StylesDecorator3",
6 "pages/AnimatableExtendDecorator/AnimatablePropertyExample",7 "pages/AnimatableExtendDecorator/AnimatablePropertyExample",
7 "pages/AnimatableExtendDecorator/AnimatablePropertyText"8 "pages/AnimatableExtendDecorator/AnimatablePropertyText"
8 ]9 ]
@@ -53,6 +53,7 @@ entry/src/main/ets/
53| | |---ExtendUsageScenario.ets53| | |---ExtendUsageScenario.ets
54| | |---ExtendUsageScenariotwo.ets54| | |---ExtendUsageScenariotwo.ets
55| | |---GlobalFunctionExtension.ets55| | |---GlobalFunctionExtension.ets
56+| | |---ExtendSupportIfelse.ets
56| | |---index.ets 57| | |---index.ets
57| |---syncStateManager //$$语法:系统组件双向同步58| |---syncStateManager //$$语法:系统组件双向同步
58| | |---SyncUsageExample.ets59| | |---SyncUsageExample.ets
@@ -0,0 +1,43 @@
1+/*
2+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+import deviceInfo from '@ohos.deviceInfo';
17+ 
18+// 在@Extend函数中使用if else实现不同版本之间的属性差异
19+@Extend(Text)
20+function someTextStyles() {
21+ // 条件表达式中支持使用deviceInfo命名空间中获取版本的属性或者方法进行判断
22+ if (deviceInfo.sdkApiVersion > 15 && deviceInfo.sdkApiVersion <= 20) {
23+ .fontColor(Color.Red)
24+ } else {
25+ .fontColor(Color.Black)
26+ }
27+}
28+ 
29+@Entry
30+@Component
31+struct LocalExtendSupportIfelse {
32+ @State message: string = 'Hello World';
33+ 
34+ build() {
35+ Column() {
36+ Text('1')
37+ Text('2')
38+ .someTextStyles()
39+ }
40+ .height('100%')
41+ .width('100%')
42+ }
43+}
@@ -23,6 +23,7 @@ import { LocalExtendUIStateVariable } from './ExtendUIStateVariable';
23import { LocalExtendUsageScenario } from './ExtendUsageScenario';23import { LocalExtendUsageScenario } from './ExtendUsageScenario';
24import { LocalExtendUsageScenario2 } from './ExtendUsageScenariotwo';24import { LocalExtendUsageScenario2 } from './ExtendUsageScenariotwo';
25import { LocalGlobalFunctionExtension } from './GlobalFunctionExtension';25import { LocalGlobalFunctionExtension } from './GlobalFunctionExtension';
26+import { LocalExtendSupportIfelse } from './ExtendSupportIfelse';
26 27 
27export const EXTEND: string = 'extend';28export const EXTEND: string = 'extend';
28 29 
@@ -54,6 +55,10 @@ const routes: Route[] = [
54 {55 {
55 name: `${EXTEND}/ExtendUsageScenariotwo`,56 name: `${EXTEND}/ExtendUsageScenariotwo`,
56 title: resource.resourceToString($r('app.string.extend_usage_scenario_two')),57 title: resource.resourceToString($r('app.string.extend_usage_scenario_two')),
58+ },
59+ {
60+ name: `${EXTEND}/ExtendSupportIfelse`,
61+ title: resource.resourceToString($r('app.string.extend_support_ifelse')),
57 }62 }
58];63];
59 64 
@@ -75,6 +80,8 @@ export function extendDestination(name: string) {
75 LocalExtendUsageScenario();80 LocalExtendUsageScenario();
76 } else if (name === `${EXTEND}/ExtendUsageScenariotwo`) {81 } else if (name === `${EXTEND}/ExtendUsageScenariotwo`) {
77 LocalExtendUsageScenario2();82 LocalExtendUsageScenario2();
83+ } else if (name === `${EXTEND}/ExtendSupportIfelse`) {
84+ LocalExtendSupportIfelse();
78 }85 }
79}86}
80 87 
@@ -100,6 +100,10 @@
100 "name": "extend_usage_scenario_two",100 "name": "extend_usage_scenario_two",
101 "value": "@Extend使用场景2"101 "value": "@Extend使用场景2"
102 },102 },
103+ {
104+ "name": "extend_support_ifelse",
105+ "value": "@Extend支持if else语句转换"
106+ },
103 {107 {
104 "name": "local",108 "name": "local",
105 "value": "@Local装饰器:组件内部状态"109 "value": "@Local装饰器:组件内部状态"