我是靠谱客的博主 安详毛衣,这篇文章主要介绍VUE3-代码抽离和组合,现在分享给大家,希望可以做个参考。

Home.vue

<script setup>
import { ref } from "vue";
import getPosts from "../composibles/getPosts";
import PostList from "../components/PostList.vue"
const {posts,load}=getPosts();
load();
</script>
<template>
<div class="home">
<div v-if="posts.length">
<PostList :posts="posts" />
</div>
<div v-else>
加载中....
</div>
</div>
</template>
<style scoped>
</style>

将获取数据的部分抽离到getPosts.js中:

import { ref } from "vue";
import axios from "axios";
const getPosts = () => {
const posts = ref([]);
const load = async() => {
try {
let { data } = await axios("http://localhost:3003/posts");
posts.value = data;
} catch (error) {
}
}
load();
return { posts ,load}
}
export default getPosts

最后

以上就是安详毛衣最近收集整理的关于VUE3-代码抽离和组合的全部内容,更多相关VUE3-代码抽离和组合内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部