ts 爬一个新闻如此简单
写网站的时候,需要新闻支持,只能爬一个咯!
import { reqNews } from '@/api/commonApi'
import { OK_CODE } from '@/app/keys'
import { ref } from 'vue'
const useNews = () => {
const news = ref([] as string[])
const refresh = () => {
reqNews().then(({ code, data }) => {
if (code === OK_CODE) {
try {
const jsonD = JSON.parse(data) as any
const dd = jsonD.data[0].content
const div = document.createElement('div')
div.innerHTML = dd
const pTag = div.querySelectorAll('p')
let arr: string[] = []
Array.prototype.slice.call(pTag).map((item) => {
arr.push(item.innerText)
})
news.value = arr
} catch (e) {}
}
})
}
return {
news,
refresh,
}
}
export default useNews