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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| <!-- * @Author: your name * @Date: 2021-12-16 13:41:59 * @LastEditTime: 2021-12-17 15:08:00 * @LastEditors: Please set LastEditors * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @FilePath: \vue1\src\App.vue --> <template> <div id="app"> <h1>app.vue</h1> <hr /> <div> <button @click="getData1(11)" :class="btnName === '个人笔记' ? 'active' : ''" > 请求个人笔记 </button> <button @click="getData1(12)" :class="btnName === '面试题' ? 'active' : ''" > 请求面试题 </button> <button @click="cancelFn">取消</button> </div> <ul v-if="list.length > 0"> <li v-for="(item, index) in list" :key="index"> 【{{ btnName }}】--- {{ item.title }} </li> </ul> <div v-else>没有相关数据</div> </div> </template>
<script> import getFn from "../axios"; import axios from "axios"; export default { name: "App", data() { return { list: [], btnName: "个人笔记", cancel: "", CancelToken: "", }; }, created() { this.CancelToken = axios.CancelToken; this.source = this.CancelToken.source(); this.getData1(); }, methods: { cancelFn() { const that = this; if (that.source) { that.source.cancel(); this.source = this.CancelToken.source(); } }, async getData1(typeId = 11) { if (typeId === 11) { this.btnName = "个人笔记"; } else { this.btnName = "面试题"; }
const that = this; if (this.cancel) { this.cancel(); }
getFn( "https://www.shaoyuhong.cn/grzx/findNoteList.php", { page: 1, type_id: typeId }, that, that.CancelToken ).then((res) => { const { code, list } = res.data; if (code === 200) { this.list = list; } }); }, }, }; </script>
<style> #app { font-family: "Avenir", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } button.active { background-color: red; color: #fff; } </style>
|