Skip to content

toStyle 样式转换 ​

17:43

说明 ​

$u.toStyle 把样式对象或样式字符串拼成可直接绑定 :style 的 CSS 字符串:对象的驼峰键会转成 kebab-case,多段入参按顺序拼接,结果统一以分号结尾。配套的 $u.splitCssProperty 按白名单把文本属性与矩形属性拆开。

示例 ​

传入对象(驼峰转短横线) ​

对象入参按 键: 值 拼接,驼峰键转 kebab-case,值原样输出(10 不会补成 10px)。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in objectList"
            :key="index"
            :title="item.desc"
            :label="item.result"
            :arrow="false"
            :border-bottom="!item.last"
        />
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type StyleRow = {
    desc: string;
    result: string;
    last: boolean;
};

// 驼峰属性名会转成 kebab-case,输出串以分号结尾
const objectList: StyleRow[] = [
    { desc: '$u.toStyle({ color: "red" })', result: $u.toStyle({ color: 'red' }), last: false },
    { desc: '$u.toStyle({ fontSize: "10px" })', result: $u.toStyle({ fontSize: '10px' }), last: false },
    { desc: '$u.toStyle({ backgroundColor: "#2979ff" })', result: $u.toStyle({ backgroundColor: '#2979ff' }), last: false },
    { desc: '$u.toStyle({ marginLeft: "5px", marginTop: "10px" })', result: $u.toStyle({ marginLeft: '5px', marginTop: '10px' }), last: true }
];
</script>

传入字符串 ​

已是样式串的入参原样保留,只在末尾补一个分号。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in stringList"
            :key="index"
            :title="item.desc"
            :label="item.result"
            :arrow="false"
            :border-bottom="!item.last"
        />
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type StyleRow = {
    desc: string;
    result: string;
    last: boolean;
};

// 已是样式串时原样保留,仅补结尾分号
const stringList: StyleRow[] = [
    { desc: '$u.toStyle("color: red")', result: $u.toStyle('color: red'), last: false },
    { desc: '$u.toStyle("color: red; font-size: 10px")', result: $u.toStyle('color: red; font-size: 10px'), last: true }
];
</script>

传入对象合并 / 字符串 ​

多个入参按顺序拼接,相同属性不会去重也不会覆盖,冲突需要调用方自行避免。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in mergeList"
            :key="index"
            :title="item.desc"
            :label="item.result"
            :arrow="false"
            :border-bottom="!item.last"
        />
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type StyleRow = {
    desc: string;
    result: string;
    last: boolean;
};

// 多参数按顺序拼接,后写者不覆盖前者,需自行保证不冲突
const mergeList: StyleRow[] = [
    { desc: '单对象:多属性拼接', result: $u.toStyle({ color: 'red', fontSize: '10px' }), last: false },
    { desc: '单属性:颜色', result: $u.toStyle({ color: 'blue' }), last: false },
    { desc: '对象 + 样式串:混传入参', result: $u.toStyle({ color: 'blue' }, 'font-size: 12px'), last: true }
];
</script>

文本与矩形属性拆分 ​

splitCssProperty 按内置文本属性白名单把 color / font-size 归入 textCssProperty,背景、内边距等其余键归入 rectCssProperty。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="textCssProperty" :label="splitText" :arrow="false" />
        <ux-cell-item title="rectCssProperty" :label="splitRect" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
    <ux-gap :height="12" />
    <view :style="splitRectStyle">
        <text :style="splitTextStyle">文本属性挂 text,矩形属性挂 view</text>
    </view>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

// 文本属性与矩形属性混在一份样式里
const splitSource = new JsonObject();
splitSource['color'] = '#ff0000';
splitSource['font-size'] = '14px';
splitSource['background-color'] = '#f5f5f5';
splitSource['padding'] = '8px';

// uni-app x 样式不继承:文本属性挂 text,矩形属性挂 view
const splitResult = $u.splitCssProperty(splitSource);
const splitText: string = JSON.stringify(splitResult.textCssProperty);
const splitRect: string = JSON.stringify(splitResult.rectCssProperty);
const splitTextStyle: string = $u.toStyle(splitResult.textCssProperty);
const splitRectStyle: string = $u.toStyle(splitResult.rectCssProperty);
</script>

样式效果预览 ​

toStyle 的返回值可直接绑定到 :style,本例用背景、圆角与边框等属性渲染一个实时预览块。

查看源码
vue
<template>
    <view class="demo-preview" :style="previewStyle">
        <text class="demo-preview-text">样式实时生效</text>
    </view>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

// toStyle 的输出可直接绑定到 :style
const previewStyle = $u.toStyle({
    backgroundColor: '#ecf5ff',
    padding: '10px',
    borderRadius: '4px',
    borderWidth: '1px',
    borderStyle: 'solid',
    borderColor: '#2979ff'
});
</script>

<style lang="scss">
.demo-preview {
    padding: 10px;
}

.demo-preview-text {
    font-size: 14px;
    color: $u-main-color;
}
</style>

API ​

toStyle ​

参数:

参数说明类型默认值版本
...styles样式对象或样式字符串,可传多个JsonObject | string | null--

返回值:

类型说明
string拼接后的 CSS 字符串,每段以 ; 结尾;无有效内容时返回 ''

splitCssProperty ​

参数:

参数说明类型默认值版本
css待拆分的样式对象JsonObject | null--

返回值:

类型说明
SplitCssPropertyResult含 textCssProperty 与 rectCssProperty 两个对象;null 入参返回两个空对象

注意事项 ​

  • 对象键按 JsonObject 遍历顺序拼接,键名中的大写字母转小写并加 -,值为 null 的项会被跳过。
  • 对象值直接拼接,不补单位、不做类型转换,数值会写成 10 而不是 10px。
  • 多个入参不会覆盖或去重,相同属性会重复出现,冲突需由调用方自行避免。
  • splitCssProperty 只按内置文本属性白名单拆分,其余键全部归入 rectCssProperty。