这是极其简化的代码:
<template>
dict_posts.length={{ dict_posts.length }}
</template>
<script lang="ts">
import axios from "axios";
export default {
setup() {
let dict_posts: { id: number; value: string }[] = [];
axios.get('http://localhost:8000/api/dict_posts').then(resp => dict_posts = resp.data);
return {dict_posts}
}
}
</script>
我需要一个要在模板中使用的职位字典。词汇量大且不可更改。据我了解,如果您将变量设为响应式,则将为此花费某些资源,而我不需要响应式,因为 他是永久的。但它是从基础异步填充的。如何仅在填充变量后将变量传递给模板?在响应式版本中,一切都很清楚。
const dict_posts = ref<{ id: number; value: string }[]>([]);
axios.get('http://localhost:8000/api/dict_posts').then(resp => dict_posts.value = resp.data);