Learn.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <div class="null" v-if="data.length === 0" tabindex="0">
  3. no information...
  4. </div>
  5. <div class="SearchLearn" v-else>
  6. <div
  7. class="row"
  8. v-for="item in data[pageSize - 1]"
  9. :key="item.id"
  10. @click="skip(item.path)"
  11. @keydown.enter.passive="skip(item.path)"
  12. tabindex="0"
  13. aria-label="Link"
  14. :aria-description="item.h3"
  15. >
  16. <div class="left">
  17. <img
  18. :src="item.cover"
  19. :alt="item.h3"
  20. tabindex="0"
  21. aria-label="Image link"
  22. :aria-description="item.h3"
  23. />
  24. </div>
  25. <div class="right">
  26. <h3 v-html="item.h3" tabindex="0" aria-label="Link"></h3>
  27. <p v-html="item.p" tabindex="0" aria-label="Link"></p>
  28. </div>
  29. </div>
  30. <!-- 分页 -->
  31. <div class="page">
  32. <span
  33. :class="{ active: pageSize === i }"
  34. v-for="i in data.length"
  35. :key="i"
  36. @click="pageChange(i)"
  37. @keydown.enter.passive="pageChange(i)"
  38. aria-label="Link"
  39. >
  40. {{ i }}
  41. </span>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import { LearnEngage } from "./data";
  47. export default {
  48. name: "SearchLearn",
  49. props: {
  50. txt: {
  51. type: String,
  52. default: "",
  53. },
  54. },
  55. components: {},
  56. data() {
  57. //这里存放数据
  58. return {
  59. data: [],
  60. pageSize: 1,
  61. };
  62. },
  63. //监听属性 类似于data概念
  64. computed: {},
  65. //监控data中的数据变化
  66. watch: {},
  67. //方法集合
  68. methods: {
  69. // 切换分页
  70. pageChange(val) {
  71. this.pageSize = val;
  72. window.scrollTo({ top: 0, behavior: "smooth" });
  73. },
  74. // 点击跳转,新窗口打开
  75. skip(path) {
  76. window.open(path, "_blank");
  77. },
  78. // 封装一个获取数据的方法
  79. getData(txt) {
  80. this.pageSize = 1;
  81. let temp = [];
  82. if (txt.trim() === "" || txt.trim().length < 4) {
  83. temp = [...LearnEngage];
  84. } else {
  85. temp = LearnEngage.filter((v) => {
  86. return (
  87. v.h3.toLowerCase().includes(txt.toLowerCase()) ||
  88. v.p.toLowerCase().includes(txt.toLowerCase())
  89. );
  90. });
  91. //
  92. temp = temp.map((v) => {
  93. return {
  94. ...v,
  95. h3: v.h3.replaceAll(txt, `<span style='color:red;'>${txt}</span>`),
  96. p: v.p.replaceAll(txt, `<span style='color:red;'>${txt}</span>`),
  97. };
  98. });
  99. }
  100. // 把结果长度给父亲显示
  101. this.$emit("update:num", temp.length);
  102. // 手动处理格式分页
  103. let pageNum = Math.ceil(temp.length / 10);
  104. let tempArrAll = [];
  105. for (let i = 0; i < pageNum; i++) {
  106. tempArrAll.push(temp.slice(i * 10, (i + 1) * 10));
  107. }
  108. this.data = tempArrAll;
  109. },
  110. searchBtn(txt) {
  111. this.getData(txt);
  112. },
  113. },
  114. //生命周期 - 创建完成(可以访问当前this实例)
  115. created() {
  116. this.getData(this.txt);
  117. },
  118. //生命周期 - 挂载完成(可以访问DOM元素)
  119. mounted() {},
  120. beforeCreate() {}, //生命周期 - 创建之前
  121. beforeMount() {}, //生命周期 - 挂载之前
  122. beforeUpdate() {}, //生命周期 - 更新之前
  123. updated() {}, //生命周期 - 更新之后
  124. beforeDestroy() {}, //生命周期 - 销毁之前
  125. destroyed() {}, //生命周期 - 销毁完成
  126. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  127. };
  128. </script>
  129. <style lang='less' scoped>
  130. .null {
  131. font-size: 30px;
  132. height: 514px;
  133. line-height: 500px;
  134. text-align: center;
  135. }
  136. .SearchLearn {
  137. padding-bottom: 20px;
  138. .row {
  139. cursor: pointer;
  140. background-color: #fff;
  141. border: 1px solid #c8c8c8;
  142. margin-bottom: 20px;
  143. padding: 20px;
  144. zoom: 1;
  145. overflow: hidden;
  146. display: flex;
  147. .left {
  148. width: 180px;
  149. text-align: center;
  150. & > img {
  151. width: 150px;
  152. }
  153. }
  154. .right {
  155. flex: 1;
  156. & > h3 {
  157. font-weight: 700;
  158. font-size: 14px;
  159. line-height: 30px;
  160. }
  161. & > p {
  162. font-size: 14px;
  163. color: #a6a6a6;
  164. line-height: 24px;
  165. }
  166. }
  167. }
  168. .page {
  169. display: flex;
  170. justify-content: center;
  171. padding-bottom: 30px;
  172. & > span {
  173. width: 30px;
  174. height: 30px;
  175. text-align: center;
  176. line-height: 30px;
  177. margin-right: 15px;
  178. border-radius: 50%;
  179. cursor: pointer;
  180. }
  181. .active {
  182. background-color: #bf2323;
  183. color: #fff;
  184. pointer-events: none;
  185. }
  186. }
  187. }
  188. </style>