0
点赞
收藏
分享

微信扫一扫

uni-app

i奇异 2022-02-27 阅读 116

uni-showToast({}) this指向undefined,无法调用data里的数据

<script>
export default {
  data () {
    return {
      searchHistoryList: [] // 搜索历史列表
    }
  },
  
  methods: {
    // 清空搜索历史记录
    clear () {
      // this.searchHistoryList = []  // 这样可以,最好给一个确认框
      // uni.setStorageSync('history', [])
      uni.showModal({
        title: '提示',
        content: '请确认是否清空搜索记录',
        success: function (res) {
          if (res.confirm) {
            // 删两个?
            // this.searchHistoryList = []  // searchHistoryList  =>undefined 
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    }
  }
}
</script>

可定义一个this指向数据,提供一种思路

<script>
export default {
methods:{
 deleteRecord () {
      this.searchHistoryList = [] // 这样可以,最好给一个确认框
      uni.setStorageSync('history', [])
      uni.showToast({
        title: '删除成功',
        duration: 1500
      })
    },
 // 清空搜索历史记录
    clear () {
      // this.searchHistoryList = []  // 这样可以,最好给一个确认框
      // uni.setStorageSync('history', [])
      let self = this
      uni.showModal({
        title: '提示',
        content: '请确认是否清空搜索记录',
        success: function (res) {
          if (res.confirm) {
            // this.searchHistoryList = []
            // uni.setStorageSync('history', [])
            self.deleteRecord()
          } else if (res.cancel) {
           // console.log('用户点击取消')
          }
        }
      })
    }
}
}
</script>
举报

相关推荐

0 条评论