<template>
<div class="app-container">
<h1>商品信息
</h1>
<div class="block">
测试Promise:
<div>
操作人{{who}},改变了商品名称:{{merchandiseName}}
<el-button @click="changeMerchandiseNameSync" type="primary">changeMerchandiseNameSync
</el-button>
</div>
<div style="margin-top:10px">
操作人{{who}},改变了商品名称:{{merchandiseName}}
<el-button @click="changeMerchandiseNameAsync" type="primary">changeMerchandiseNameAsync
</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
merchandiseName:'default',
who:'default'
}
},
methods:{
changeMerchandiseNameAsync(){
this.requestMerchandiseName().then(responseMesssage=>{
this.merchandiseName=responseMesssage;
}).catch(error=>{
alert(error);
});
this.who='张三';
},
async changeMerchandiseNameSync(){
const responseMesssage = await this.requestMerchandiseName().catch(error=>{
alert(error);
});
this.merchandiseName=responseMesssage;
this.who='张三';
},
requestMerchandiseName(){
return new Promise((resolver,reject)=>{
setTimeout(() => {
resolver('i am learning');
}, (3000));
})
}
}
}
</script>
<style scoped>
.block{
border: solid 1px #ccc;
padding: 20px;
}
</style>