// my-component.js // 引入全局 Vue 对象(因在 HTML 中通过 script 引入,Vue 已挂载到 window) const { defineComponent, ref, onMounted, nextTick } = Vue; // 定义组件(直接使用模板) export const latestList = defineComponent({ name: "latestList", props: { boxtype: { type: String, default: "", }, itemdata: { type: Object, default: () => {}, }, }, setup(props) { let item = ref({ ...props.itemdata }); let postsTab = ref("newest"); // newest essence let boxtype = ref(props.boxtype); onMounted(() => { if (props.boxtype == "newest") getTopicLatest(); else if (props.boxtype == "essence") topicHandpicked(); else { getTopicLatest(); topicHandpicked(); } }); let count = 0; let latestList = ref([]); const getTopicLatest = () => { ajaxGet(`/v2/api/forum/getTopicLatest?type=thread`).then((res) => { const data = res.data; const list = data.thread || []; latestList.value = list; nextTick(() => { count += 1; examineCount(); if (props.boxtype == "newest") { tabPostsItem("newest"); } }); }); }; let topList = ref([]); const topicHandpicked = () => { ajaxGet(`/v2/api/forum/getHomebestRecommend?limit=15&type=thread`).then((res) => { const data = res.data; console.log('data', data); topList.value = data; nextTick(() => { count += 1; examineCount(); if (props.boxtype == "essence") tabPostsItem("essence"); }); }); }; // 检验 count 是否等于 2 function examineCount() { if (count == 2) tabPostsItem(tabPostsArr[Math.floor(Math.random() * tabPostsArr.length)]); } const tabPostsArr = ["newest", "essence"]; const tabPostsItem = (key) => { if (key == postsTab.value) return; const boxbox = document.querySelector(`.posts-box.boxtype-${boxtype.value}`); boxbox.classList.add(`box-${key}`); boxbox.classList.remove(`box-${postsTab.value}`); postsTab.value = key; }; return { boxtype, tabPostsItem, postsTab, topList, latestList, item }; }, components: { }, template: `
最新
精华阅读
最新
精华
`, });