:on-success无法传值吗
时间: 2023-06-27 12:08:37 浏览: 70
`on-success` 是一个 Vue.js 指令,用于在 Promise 成功时执行一些操作。它不能直接传递值,但可以通过在 `Promise` 的 `resolve` 方法中返回一个值来间接传递值。例如:
```html
<template>
<div>
<button @click="fetchData">Fetch data</button>
<div v-if="data">{{ data }}</div>
</div>
</template>
<script>
export default {
data() {
return {
data: null
};
},
methods: {
fetchData() {
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(response => response.json())
.then(data => {
this.data = data;
return data; // 将数据返回给 resolve 方法
})
.then(this.onSuccess); // 调用 onSuccess 方法并将数据作为参数传递
},
onSuccess(data) {
console.log(data); // 输出获取的数据
}
}
};
</script>
```
在上面的例子中,当成功获取数据后,`fetchData` 方法会将数据存储在组件的 `data` 属性中,并且将数据作为参数返回给 `resolve` 方法。然后,`onSuccess` 方法会被调用,并将数据作为参数传递。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)