一、使用ref
1. 引入
从vue里面引入ref函数
import {defineComponent, onMounted, ref} from 'vue';
const ebooks = ref();
ebooks.value = data.content;
return {
ebooks
};
- 2
- 3
5. 将数据渲染到页面
{{ebooks}}
import {defineComponent, onMounted, ref,reactive,toRef} from 'vue';
const ebooks1 = reactive({books: []});
ebooks1.books = data.content;
return {
booklist : toRef(ebooks1,'books')
};
- 2
- 3
5. 将数据渲染到页面
{{booklist }}
<template>
<a-layout>
<a-layout-sider width="https://files.jxasp.com/image/200" style="background: #fff">
<a-menu
mode="inline"
v-model:selectedKeys="selectedKeys2"
v-model:openKeys="openKeys"
:style="{ height: '100%', borderRight: 0 }"
>
<a-sub-menu key="sub1">
<template #title>
<span>
<user-outlined/>
subnav 1
</span>
</template>
<a-menu-item key="1">option1</a-menu-item>
<a-menu-item key="https://files.jxasp.com/image/2">option2</a-menu-item>
<a-menu-item key="3">option3</a-menu-item>
<a-menu-item key="4">option4</a-menu-item>
</a-sub-menu>
<a-sub-menu key="sub2">
<template #title>
<span>
<laptop-outlined/>
subnav 2
</span>
</template>
<a-menu-item key="5">option5</a-menu-item>
<a-menu-item key="6">option6</a-menu-item>
<a-menu-item key="7">option7</a-menu-item>
<a-menu-item key="8">option8</a-menu-item>
</a-sub-menu>
<a-sub-menu key="sub3">
<template #title>
<span>
<notification-outlined/>
subnav 3
</span>
</template>
<a-menu-item key="9">option9</a-menu-item>
<a-menu-item key="10">option10</a-menu-item>
<a-menu-item key="11">option11</a-menu-item>
<a-menu-item key="12">option12</a-menu-item>
</a-sub-menu>
</a-menu>
</a-layout-sider>
<a-layout-content
:style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
>
<pre>
{{ebooks}}
</pre>
<pre>
{{booklist}}
</pre>
</a-layout-content>
</a-layout>
</template>
<script lang="ts">
import {UserOutlined, LaptopOutlined, NotificationOutlined} from '@ant-design/icons-vue';
import axios from 'axios';
import {defineComponent, onMounted, ref,reactive,toRef} from 'vue';
export default defineComponent({
name: 'Home',
components: {
UserOutlined,
LaptopOutlined,
NotificationOutlined,
},
setup() {
console.log('setup');
//使用ref函数
const ebooks = ref();
//使用reactive函数 定义接收返回对象的变量以及类型
const ebooks1 = reactive({books: []});
//声明周期函数
onMounted(() => {
console.log('onMounted')
axios.get("http://localhost:8888/ebook/list?name=Spring").then((response) => {
const data = response.data;
// 使用ref 接收返回值
ebooks.value = data.content;
//使用reactive 接收返回值
ebooks1.books = data.content;
console.log(response);
});
});
return {
// 使用ref 将返回值返回
ebooks,
//使用reactive 将返回值返回
booklist : toRef(ebooks1,'books')
};
}
});
</script>
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105