export function useAuthInfo() {
const userData = ref(null);
const token = ref(null);
const login = (loginData: LoginData) => {
fetch('/login', {body: loginData, method: 'POST'})
.then(res => res.json())
.then(res => {
userData.value = res.data.userData;
token.value = res.data.token;
})
}
const logOut = () => {
fetch('logout')
.then(_ => { userData.value = null; token.value = null })
}
return {
userData,
token,
login,
logout
}
}
//app.vue
import { useAuthInfo } from 'src/vca';
import { useProvider } from 'vue-vulcan';
<script lang="ts" setup>
useProvider(useAuthInfo);
</script>
//userData.vue
import { useAuthInfo } from 'src/vca';
import { useInjector } from 'vue-vulcan';
<script lang="ts" setup>
const { userData } = useInjector(useAuthInfo);
</script>
<template>
<div>
<span>userData.username</span>
<span>userData.age</span>
</div>
</template>
export function useBookList() {
const [ bookList ] = useRequest('/getBookList', { data: {groupId: '1'} });
return {
bookList
}
}
export function useBookList() {
const [bookList, getBookList] = useRequest('/getBookList', { data: {groupId: '1'}, auto: false });
return {
bookList,
getBookList
}
}