提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
666 B

3 weeks ago
  1. import {defineStore} from 'pinia'
  2. import axios from 'axios'
  3. import {nanoid} from 'nanoid'
  4. export const useTalkStore = defineStore('talk',{
  5. actions:{
  6. async getATalk(){
  7. // 发请求,下面这行的写法是:连续解构赋值+重命名
  8. let {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
  9. // 把请求回来的字符串,包装成一个对象
  10. let obj = {id:nanoid(),title}
  11. // 放到数组中
  12. this.talkList.unshift(obj)
  13. }
  14. },
  15. // 真正存储数据的地方
  16. state(){
  17. return {
  18. talkList:JSON.parse(localStorage.getItem('talkList') as string) || []
  19. }
  20. }
  21. })