getState(options)
获取共享状态值
options
| 参数名称 | 类型 | 必填 | 可选值 | 默认值 | 描述 |
|---|---|---|---|---|---|
| key | string | 是 | - | - | 获取共享状态值的键(key)值,在 setState 设置时的键(key) |
| path | string | 否 | - | - | 数据对象路径 |
基础使用
vue
<template>
<view>
<!-- 显示:测试值 -->
{{ucsShare.getState("test")}} // [!code ++]
</view>
</template>
<script setup>
import ucsShare from "@/uni_modules/ucs-share";
/**
* 示例设置共享状态键值
*/
ucsShare.setState("test","测试值");
</script>path参数使用
path 参数是在取 object 数据类型时取某单个对象时需要用到的参数
基础使用
vue
<template>
<view>
<!-- 显示:参数1 -->
{{ucsShare.getState("test","param1")}} // [!code ++]
</view>
</template>
<script setup>
import ucsShare from "@/uni_modules/ucs-share";
/**
* 示例设置共享状态键值
*/
const data = {
param1: "参数1"
};
ucsShare.setState("test", data);
</script>多层数据使用
当 object 数据存在多级时,可以使用 . 的方式进行下一层数据的选择
vue
<template>
<view>
<!-- 显示:参数3 -->
{{ucsShare.getState("test","param2.param3")}} // [!code ++]
</view>
</template>
<script setup>
import ucsShare from "@/uni_modules/ucs-share";
/**
* 示例设置共享状态键值
*/
const data = {
param1: "参数1",
param2: {
param3: "参数3"
}
};
ucsShare.setState("test", data);
</script>数组数据使用
当 object 数据中存在数组需要取数组中的某一下标的值时,可以使用 .[0] 的方式,其中 0 指的是数组内的下标
vue
<template>
<view>
<!-- 显示:参数3 -->
{{ucsShare.getState("test","param2.[1]")}} // [!code ++]
</view>
</template>
<script setup>
import ucsShare from "@/uni_modules/ucs-share";
/**
* 示例设置共享状态键值
*/
const data = {
param1: "参数1",
param2: ["参数2","参数3","参数4"]
};
ucsShare.setState("test", data);
</script>