Learn.vue 3.7 KB

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