我是靠谱客的博主 标致黑猫,这篇文章主要介绍quasar + vue3 composition api 使用refs内方法以及使用挂载api(axios)的方法记录quasar + vue3 composition api 初步应用,现在分享给大家,希望可以做个参考。
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内的方法,仅供参考(估计写法不是很规范):
复制代码
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70<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的方法,如下:
复制代码
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
41<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>
最后
以上就是标致黑猫最近收集整理的关于quasar + vue3 composition api 使用refs内方法以及使用挂载api(axios)的方法记录quasar + vue3 composition api 初步应用的全部内容,更多相关quasar内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复