quasar + vue3 composition api 使用refs内方法以及使用挂载api(axios)的方法记录

quasar + vue3 composition api 初步应用

你好! 这记录一次初步应用学习quasar + vue3 Composition api,应用场景是 a p i ( api( api(axios)挂载,但是不使用Options API的情况。

应用例子

以下为学习应用例子,通过getCurrentInstance 获取proxy,来使用挂载的 a p i ( api( api(axios),以及refs内的方法,仅供参考(估计写法不是很规范):

<template>
  <div class="flex flex-start q-pa-md non-selectable">
    <q-infinite-scroll @load="getList" :reverse="false" :offset="5"  class="infinite-panel" ref="infiniteScroll">
      <template v-slot:loading>
        <div class="row justify-center q-my-md">
          <q-spinner color="primary" name="dots" size="40px" />
        </div>
      </template>
      <q-timeline color="secondary">
        <q-timeline-entry title="" :subtitle="news.createTime" v-for="(news,i) in newsList" :key="i">
          <a href="news.sourceUrl">{{news.sourceUrl}}</a>
          <div style="white-space: pre-wrap;">{{news.text}}</div>
          <div style="white-space: pre-wrap;">{{news.textCn}}</div>
          <div v-for="(item,index) in news.urlList" :key="`${index + '*'}`">
            {{item}}
          </div>
        </q-timeline-entry>
      </q-timeline>
    </q-infinite-scroll>

  </div>
</template>

<script>
import { defineComponent, ref, reactive, onMounted, getCurrentInstance } from "vue";
export default defineComponent({
  name: "twitterNews",
  setup() {
    let { proxy } = getCurrentInstance()
    let newsList = reactive([])
    //获取list
    const getList = (index, done) => {
      proxy.$api({
        method: "get",
        url: "twitters/list" + `?current=${index}`,
      })
        .then((res) => {
          if(res.data.code === 0){
            newsList.push(...res.data.data.records);
            if(res.data.data.records.length === 0){
              try {
                proxy.$refs.infiniteScroll.stop()
              } catch (error) {}
            } 
          }
          done()
        })
        .catch((e) => {
          console.log(e)
          done()
        });
    }
    onMounted(()=>{
    })
    return {
      newsList,
      getList
    };
  },
});
</script>

<style scoped>
.infinite-panel{
  height: calc(100% - 80px);
}
.infinite-panel > ul {
  margin-top:0;
  margin-bottom:0;
}
</style>

以下再提供一种可能较为规范和普遍的 a p i ( api( api(axios)通过引入使用,然后通过ref来使用refs的方法,如下:

<script>
import { api } from "../boot/axios.js"
import { defineComponent, ref, reactive, onMounted } from "vue";
export default defineComponent({
  name: "twitterNews",
  setup() {
    const infiniteScroll = ref(null)
    let newsList = reactive([])
    //获取list
    const getList = (index, done) => {
      api({
        method: "get",
        url: "twitters/list" + `?current=${index}`,
      })
        .then((res) => {
          if(res.data.code === 0){
            newsList.push(...res.data.data.records);
            if(res.data.data.records.length === 0){
              try {
                infiniteScroll.value.stop()
              } catch (error) {}
            } 
          }
          done()
        })
        .catch((e) => {
          console.log(e)
          done()
        });
    }
    onMounted(()=>{
    })
    return {
      infiniteScroll,
      newsList,
      getList
    };
  },
});
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值