onLoad(option) { setTimeout(function() { this.$eventBus.$on('addtohuodong',(res) => { console.log(res,'接收到的数据') this.test = res console.log(this.hudongform.content.cost,'hudongform中cost接收到的数据') console.log(this.hudongform.content.cost.length,'hudongform中cost的长度') }) }, 3000); },为什么报错
时间: 2024-03-24 21:38:36 浏览: 20
在这段代码中,`this`指的是`setTimeout`函数的作用域,而不是Vue实例的作用域,所以在`this.$eventBus.$on`中无法访问Vue实例中的数据或方法。
为了解决这个问题,可以使用箭头函数来代替`function`,因为箭头函数不会改变`this`的作用域。修改后的代码如下:
```
onLoad(option) {
setTimeout(() => {
this.$eventBus.$on('addtohuodong', (res) => {
console.log(res, '接收到的数据')
this.test = res
console.log(this.hudongform.content.cost, 'hudongform中cost接收到的数据')
console.log(this.hudongform.content.cost.length, 'hudongform中cost的长度')
})
}, 3000);
},
```
这样就可以在箭头函数中访问Vue实例中的数据和方法了。
相关问题
export default { data() { return { technolog: {}, } }, onLoad(option) { var id=option.id console.log(option) this.getTech(id) }, methods: { // 获取列表数据 getTech(id) { uni.$api.type.technolog(id) .then(res => { console.log('科技成果数据列表:', res) this.technolog = res.Data id=res.Data.Id }).catch(error => { console.log(error) }) }, 让getTech方法实现通过不同的id实现不同数据展示
要让 `getTech` 方法根据不同的 `id` 显示不同的数据,可以在调用 `uni.$api.type.technolog` 方法时,将传入的 `id` 参数作为接口请求的参数,以获取不同的数据。例如:
```
getTech(id) {
uni.$api.type.technolog(id)
.then(res => {
console.log('科技成果数据列表:', res)
this.technolog = res.Data
// 在这里可以根据获取到的数据做一些处理
}).catch(error => {
console.log(error)
})
}
```
在这个例子中,使用 `id` 参数作为接口请求的参数,获取到的数据将赋值给 `technolog` 对象,并可以根据获取到的数据做一些处理。例如,可以根据获取到的数据在页面中渲染不同的内容。
export default { data() { return { technolog: {}, } }, onLoad(option) { var id=option.id console.log(option) this.getTech(id) }, methods: { // 获取列表数据 getTech(id) { uni.$api.type.technolog(id) .then(res => { console.log('科技成果数据列表:', res) this.technolog = res.Data id=res.Data.Id }).catch(error => { console.log(error) }) }, 让getTech方法实现通过不同的id实现当前id存在数据展示
要让 `getTech` 方法通过不同的 `id` 显示当前 `id` 存在的数据,可以先判断获取到的数据是否包含当前 `id`,如果包含则将数据赋值给 `technolog` 对象,并进行相应的处理,如果不包含则进行提示或清空 `technolog` 对象。例如:
```
getTech(id) {
uni.$api.type.technolog(id)
.then(res => {
console.log('科技成果数据列表:', res)
if (res.Data.Id === id) {
this.technolog = res.Data
// 在这里可以根据获取到的数据做一些处理
} else {
uni.showToast({
title: '当前id不存在数据',
icon: 'none'
})
// 清空technolog对象
this.technolog = {}
}
}).catch(error => {
console.log(error)
})
}
```
在这个例子中,先判断获取到的数据的 `Id` 是否与传入的 `id` 相同,如果相同则将数据赋值给 `technolog` 对象,并进行相应的处理;如果不相同则进行提示并清空 `technolog` 对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)