# uni.env

环境变量

uni.env提供了一些环境变量,主要是App和小程序文件系统相关的。理解这些环境需要参考文档:文件系统

# env 的属性值

名称 类型 必备 默认值 兼容性 描述
USER_DATA_PATH string -
文件系统中的用户文件目录路径
CACHE_PATH string -
文件系统中的缓存文件目录路径
SANDBOX_PATH string -
文件系统中的应用沙盒目录路径
TEMP_PATH string -
文件系统中的应用临时目录路径,应用退出自动清理
ANDROID_INTERNAL_SANDBOX_PATH string -
文件系统中的应用内置沙盒目录路径(仅app-android平台支持)

# 参见

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

扫码体验(手机浏览器跳转到App直达页)
<template>
  <view style="margin:12px">
    <page-head title="环境变量 - 文件系统"></page-head>
    <button class="button" type="primary" @tap="getDirInfo(uni.env.USER_DATA_PATH)">USER_DATA_PATH</button>
    <button class="button" type="primary" @tap="getDirInfo(data.cachePath)">CACHE_PATH</button>
    <button class="button" type="primary" @tap="getDirInfo(data.sandboxPath)">SANDBOX_PATH</button>
    <!-- #ifdef APP-HARMONY -->
    <button class="button" type="primary" @tap="getDirInfo(data.tempPath)">TEMP_PATH</button>
    <!-- #endif -->
    <!-- #ifdef APP-ANDROID -->
   <button class="button" type="primary" @tap="getDirInfo(data.androidInternalSandboxPath)">ANDROID_INTERNAL_SANDBOX_PATH</button>
    <!-- #endif -->
    <boolean-data :defaultValue="false" title="是否递归获取" @change="switchRecursive"></boolean-data>
  </view>
  <scroll-view style="flex: 1; padding: 16px 0px;">
    <text class="result">{{data.result}}</text>
    <text class="error">{{data.error}}</text>
    <view class="stat" v-for="(stat,index) in data.list" :key="index" >
      <text class="path">{{stat.path}}</text>
      <text class="size">{{stat.size}}</text>
    </view>
  </scroll-view>
</template>

<script setup lang="uts">
  type StatInfo = {
    path : string;
    size : string;
  };

  type DataType = {
    result: string;
    error: string;
    list: Array<StatInfo>;
    recursive: boolean;
    cachePath: string;
    sandboxPath: string;
    // #ifdef APP-HARMONY
    tempPath: string;
    // #endif
    // #ifdef APP-ANDROID
    androidInternalSandboxPath: string;
    // #endif
  }

  const data = reactive({
    result: '',
    error: '',
    list: [],
    recursive: false,
    cachePath: uni.env.CACHE_PATH,
    sandboxPath: uni.env.SANDBOX_PATH,
    // #ifdef APP-HARMONY
    tempPath: uni.env.TEMP_PATH,
    // #endif
    // #ifdef APP-ANDROID
    androidInternalSandboxPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH,
    // #endif
  } as DataType)

  const switchRecursive = () => {
    data.recursive = !data.recursive
  }

  const getDirInfo = (dirPath: string) => {
    const fm = uni.getFileSystemManager()
    data.list = [];
    fm.stat({
      path: dirPath,
      recursive: data.recursive,
      success: (res: StatSuccessResult) => {
        data.result = `获取 "${dirPath}" 成功(success)`
        console.log(data.result)
        res.stats.forEach((item)=>{
          data.list.push({
            path: item.path,
            size: `${item.stats.size} Bytes`
          } as StatInfo)
        })
      },
      fail: (err) => {
        data.result = `获取 "${dirPath}" 失败(fail)`
        console.log(data.result)
        data.error = JSON.stringify(err)
        console.log(data.error)
      }
    })
  }

</script>

<style>
  .button {
    margin-bottom: 4px;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .result {
    font-size: 18px;
    font-weight: bold;
    text-align: center;
    width: 100%;
  }
  .error {
    color: firebrick;
  }
  .stat {
    padding: 8px 16px;
  }
  .path {
    color: darkgray;
  }
  .size {
    color: darkgrey;
  }
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息