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存在数据展示
时间: 2024-03-23 09:38:01 浏览: 19
要让 `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` 对象。
相关问题
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` 对象,并可以根据获取到的数据做一些处理。例如,可以根据获取到的数据在页面中渲染不同的内容。