unicloud-import-object.uvue 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
    <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodo">添加Todo</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="randomFail">随机触发失败重试</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="fail">云对象失败调用</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: '请求云对象'
      }
    },
    methods: {
      addTodo() {
        const todo = uniCloud.importObject('todo')
        const title = '学习编程'
        const content = '熟悉uts语法'
        todo.add(title, content).then<void>((res: UTSJSONObject) : void => {
          uni.showModal({
雪洛's avatar
雪洛 已提交
41
            title: '提示',
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            content: res['showMessage'] as string,
            showCancel: false
          })
        }).catch<void>((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      },
      randomFail() {
        const todoObj = uniCloud.importObject('todo', {
          errorOptions: {
            retry: true
          }
        })
        todoObj.randomFail().then<void>((res: UTSJSONObject) : void => {
          uni.showModal({
雪洛's avatar
雪洛 已提交
58
            title: '提示',
59 60 61 62 63 64 65 66 67 68 69 70
            content: res['showMessage'] as string,
            showCancel: false
          })
        }).catch<void>((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      },
      fail() {
        const todo = uniCloud.importObject('todo')
        todo.fail().then<void>((res: UTSJSONObject) : void => {
          uni.showModal({
雪洛's avatar
雪洛 已提交
71
            title: '提示',
72 73 74 75 76 77 78 79 80 81 82 83 84
            content: 'todo.fail应调用失败,此处错误的触发了成功回调',
            showCancel: false
          })
          console.log('todo.fail: ', res);
        }).catch<void>((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      },
      success() {
        const todo = uniCloud.importObject('todo')
        todo.success().then<void>((res: UTSJSONObject) : void => {
          uni.showModal({
雪洛's avatar
雪洛 已提交
85
            title: '提示',
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            content: res['showMessage'] as string,
            showCancel: false
          })
        }).catch<void>((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      }
    }
  }
</script>

<style>

</style>