<!
DOCTYPE
html>
<
html
lang
=
"zh-CN"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>TEST</
title
>
<
script
src
=
"https://candyissupersweet.gitee.io/cdn/vue3/vue.global.prod.js"
></
script
>
</
head
>
<
body
>
<
div
id
=
"app"
> </
div
>
<
script
type
=
"text/javascript"
>
let that = null, App = null;
App = {
mounted() {
that = this;
that.start();
},
methods: {
async start() {
const myData = await that.getData();
console.log('开始使用数据');
console.log('myData', myData);
},
getData() {
return new Promise((res, rej) => {
try {
const xhr = new XMLHttpRequest();
xhr.open('get', '/data.txt', true);
xhr.onload = () => {
res(handleData(xhr.response))
};
xhr.send();
} catch (error) {
// 【备用数据步骤】
// 当出现跨域请求等错误导致无法获得数据时,用备用数据。
// 这里一些逻辑生成data
// 用户上传打开文件
const data = [];
res(handleData(data))
}
// 处理数据
function handleData(data) {
// 加工一下数据
return data
}
})
}
}
}
const app = Vue.createApp(App);
app.mount("#app");
</
script
>
</
body
>
</
html
>