
简体中文
环境变量
uni.env提供了一些环境变量,主要是App和小程序文件系统相关的。理解这些环境需要参考文档:文件系统
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
USER_DATA_PATH | string | 是 | - | 文件系统中的用户文件目录路径 | |
CACHE_PATH | string | 是 | - | 文件系统中的缓存文件目录路径 | |
SANDBOX_PATH | string | 是 | - | 文件系统中的应用沙盒目录路径 | |
TEMP_PATH | string | 是 | - | 文件系统中的应用临时目录路径,应用退出自动清理 | |
ANDROID_INTERNAL_SANDBOX_PATH | string | 是 | - | 文件系统中的应用内置沙盒目录路径(仅app-android平台支持) |
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<view style="margin:12px">
<page-head title="环境变量 - 文件系统"></page-head>
<button class="button" type="primary" @tap="getDirInfo(userDataPath)">USER_DATA_PATH</button>
<button class="button" type="primary" @tap="getDirInfo(cachePath)">CACHE_PATH</button>
<button class="button" type="primary" @tap="getDirInfo(sandboxPath)">SANDBOX_PATH</button>
<!-- #ifdef APP-HARMONY -->
<button class="button" type="primary" @tap="getDirInfo(tempPath)">TEMP_PATH</button>
<!-- #endif -->
<!-- #ifdef APP-ANDROID -->
<button class="button" type="primary" @tap="getDirInfo(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">{{result}}</text>
<text class="error">{{error}}</text>
<view class="stat" v-for="(stat,index) in list" :key="index" >
<text class="path">{{stat.path}}</text>
<text class="size">{{stat.size}}</text>
</view>
</scroll-view>
</template>
<script>
type StatInfo = {
path : string;
size : string;
};
export default {
data() {
return {
result: '',
error: '',
list: [] as Array<StatInfo>,
recursive: false,
userDataPath: uni.env.USER_DATA_PATH as string,
cachePath: uni.env.CACHE_PATH as string,
sandboxPath: uni.env.SANDBOX_PATH as string,
// #ifdef APP-HARMONY
tempPath: uni.env.TEMP_PATH as string,
// #endif
// #ifdef APP-ANDROID
androidInternalSandboxPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH as string,
// #endif
}
},
methods: {
switchRecursive() {
this.recursive = !this.recursive
},
getDirInfo(dirPath:string) {
const fm = uni.getFileSystemManager()
this.list = [];
fm.stat({
path: dirPath,
recursive: this.recursive,
success: (res: StatSuccessResult) => {
this.result = `获取 "${dirPath}" 成功(success)`
console.log(this.result)
res.stats.forEach((item)=>{
this.list.push({
path: item.path,
size: `${item.stats.size} Bytes`
})
})
},
fail: (err) => {
this.result = `获取 "${dirPath}" 失败(fail)`
console.log(this.result)
this.error = JSON.stringify(err)
console.log(this.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>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |