Closed TenviLi closed 2 years ago
// 创建 input 元素选择 json 文件
static async loadJSONFile<T>(): Promise<T> {
return new Promise<T>((resolve, reject) => {
const i = document.createElement('input')
i.setAttribute('type', 'file')
i.setAttribute('accept', 'application/json')
i.onchange = () => {
if (i.files && i.files.length > 0) {
// 读取文件内容
const file = new FileReader()
file.readAsText(i.files[0])
file.onload = () => {
const str = file.result as string
let result: T
try {
result = JSON.parse(str)
// if((result as any).constructor !== Object){
// 允许是对象 {} 或者数组 []
if (result === null || typeof result !== 'object') {
const msg = 'Data is not an object!'
return reject(new Error(msg))
}
return resolve(result)
} catch (error) {
const msg = 'JSON parse error!'
return reject(new Error(msg))
}
}
}
}
i.click()
})
}
from
场景:
功能点: