作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我有以下代码可以正常工作:
// api.js
export default {
async readAsync (resource) {
await new Promise(r => setTimeout(r, 400));
return data[resource];
},
}
// ProductList.vue
import api from '@/services/api'
[...]
methods: {
fetch () {
this.loading = true;
api.readAsync('products').then(data => {
this.products = data;
this.loading = false;
});
}
}
[...]
我想使用 await 来摆脱 promise ,就像这样:
methods: {
fetch () {
this.loading = true;
this.products = await api.readAsync('products');;
this.loading = false;
}
}
但是我得到以下错误:
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: D:\devel\apps\vue-tests\cli-test\src\components\ProductList.vue: await is a reserved word (59:22)
57 | this.loading = true;
58 |
> 59 | this.products = await api.readAsync('products');;
| ^
60 | this.loading = false;
知道我做错了什么吗?
我是一名优秀的程序员,十分优秀!