在vue2中一般放在created中,但是在vue3中取消了created生命周期,请求方式有两种:
- 直接在setup中去获取数据
setup(props) { const data = reactive({ note: [], }); getArticalLatest().then((res) => { data.note = res.data.data; }); return { ...toRefs(data), }; },
- 在onMouted里面请求数据
setup(props) { const data = reactive({ note: [], }); onMounted(() => { getArticalLatest().then((res) => { data.note = res.data.data; }); }); return { ...toRefs(data), }; },
<script setup name="User"> /* onMounted(() => { // 角色 let reas = getRole() console.info(reas.data) }); */ onMounted(async () => { // 角色 let reas = await getRole() roleOptions.value = reas.data console.info(reas.data) }); </script>