vue3 hooks应用

 1新建一个hooks文件夹 下面写2个文件

1个是useDog.js

import axios from 'axios'
import { ref } from 'vue'
export default function(){
let dog = ref({
  id: 1,
  name: '哈士奇'
})

let imgs = ref([])
const changeDog = () => {
  dog.value.name += '土狗'
  axios.get('https://dog.ceo/api/breed/pembroke/images/random').then((res) => {
    console.log(res.data.message)
    imgs.value.push({ src: res.data.message })
  })
}
return {dog,imgs,changeDog}
}

1个是useStudent.js

import { ref } from 'vue'
export default function(){
    let student = ref({
        id: 1,
        name: '张三'
    })

    const changeName = () => {
    student.value.name += '李四'
    }

return{student,changeName}
}

 

2页面引入使用