使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
一、與 Vue2 對比
1、 Vue3 新特性
- 數(shù)據(jù)響應重新實現(xiàn)(
ES6的proxy代替Es5的Object.defineProperty) - 源碼使用
ts重寫,更好的類型推導 - 虛擬
DOM新算法(更快,更?。?/li> - 提供了
composition api,為更好的邏輯復用與代碼組織 - 自定義渲染器(
app、小程序、游戲開發(fā)) Fragment,模板可以有多個根元素
2、 Vue2、Vue3 響應原理對比
Vue2 使用 Object.defineProperty 方法實現(xiàn)響應式數(shù)據(jù)
缺點:
- 無法檢測到對象屬性的動態(tài)添加和刪除
- 無法檢測到數(shù)組的下標和
length屬性的變更
解決方案:
Vue2提供Vue.$set動態(tài)給對象添加屬性Vue.$delete動態(tài)刪除對象屬性
3、重寫數(shù)組的方法,檢測數(shù)組變更
Vue3 使用 proxy 實現(xiàn)響應式數(shù)據(jù)
優(yōu)點:
- 可以檢測到代理對象屬性的動態(tài)新增和刪除
- 可以見到測數(shù)組的下標和
length屬性的變化
缺點:
es6的proxy不支持低版本瀏覽器 IE11- 回針對 IE11 出一個特殊版本進行支持
以上引用《[vue2和vue3比較]》( https://www.cnblogs.com/yaxinwang/p/13800734.html )
4、直觀感受
目前實際工作中還是以Vue2為主
Vue3 包含 mounted、data、methods ,被一個 setup() 全給包了
二、使用Vue3進行數(shù)據(jù)綁定示例
上一篇Vue3 集成HTTP庫axios詳情我們已經(jīng)實現(xiàn)了將后臺返回數(shù)據(jù),在前臺頁面展示了(雖然是在控制臺),但這也只能說明完成了90%。
接下來就是我們怎么把后臺接口返回數(shù)據(jù),怎么展示到頁面的過程了。
1、使用ref實現(xiàn)數(shù)據(jù)綁定
我們還是需要在 home 里面修改,畢竟在頁面展示,所以只需修改 Home 部分代碼,具體示例代碼如下:
<template>
<a-layout>
<a-layout-sider width="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="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' }"
>
{{ebooks}}
<pre>
{{ebooks}}
</pre>
</a-layout-content>
</a-layout>
</template>
<script lang="ts">
import { defineComponent,onMounted,ref } from 'vue';
import axios from 'axios';
export default defineComponent({
name: 'Home',
setup(){
console.log('set up');
const ebooks=ref();
onMounted(()=>{
axios.get("http://localhost:8888/ebook/list?name=spring").then(response =>{
console.log("onMounted");
const data=response.data;
ebooks.value=data.content;
})
})
return{
ebooks
}
}
});
</script>
知識點:
const ebooks=ref(); 這是一個響應式數(shù)據(jù),而Vue3新增了 ref ,用來定義響應式數(shù)據(jù),也就是說ebooks是實時的數(shù)據(jù)展示;ref對應的賦值是value;- 使用 {{變量}} 取值;
重新編譯,啟動服務,查看效果如下:

2、使用reactive實現(xiàn)數(shù)據(jù)綁定
同樣,還是在 home 里面修改,示例代碼如下:
<template>
<a-layout>
<a-layout-sider width="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="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' }"
>
<strong>使用ref進行數(shù)據(jù)綁定結果:</strong><p></p>
{{ebooks1}}
<p></p>
<pre>
{{ebooks1}}
</pre>
<strong>使用reactivef進行數(shù)據(jù)綁定結果:</strong><p></p>{{ebooks2}}
<p></p>
<pre>
{{ebooks2}}
</pre>
</a-layout-content>
</a-layout>
</template>
<script lang="ts">
import { defineComponent,onMounted,ref,reactive,toRef} from 'vue';
import axios from 'axios';
export default defineComponent({
name: 'Home',
setup(){
console.log('set up');
//使用ref進行數(shù)據(jù)綁定
const ebooks=ref();
// 使用reactive進行數(shù)據(jù)綁定
const ebooks1=reactive({books:[]})
onMounted(()=>{
axios.get("http://localhost:8888/ebook/list?name=spring").then(response =>{
console.log("onMounted");
const data=response.data;
ebooks.value=data.content;
ebooks1.books=data.content;
})
})
return{
ebooks1: ebooks,
ebooks2:toRef(ebooks1,"books")
}
}
});
</script>
知識點:
需要從 vue 中導入 reactive , toRef ;
reactive({books:[]}) 中 reactive 的 () 中必須存放的是對象,此處我用 books 里面加了個空集合;
toRef(ebooks1,"books") 中,是將books變?yōu)轫憫阶兞浚?br />
顯然使用 reactive 比較麻煩,項目實際開發(fā)中必須統(tǒng)一,不能既使用 reactive 又使用 ref ;
用 ref 比較麻煩的是,使用變量的話,不管是獲取還是使用的話都需要加上 .value ;
重新編譯,啟動服務,查看效果如下:

三、寫在最后
還是前端部分開發(fā)給人的成就感更直觀,因為直觀可以看到,不像 controller 或者 service 中業(yè)務邏輯代碼一樣,寫了好多,也看不出個所以然,但這也不影響我對 coding 的喜歡。
到此這篇關于使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)的文章就介紹到這了,更多相關Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Vue+elementUI實現(xiàn)動態(tài)展示列表的數(shù)據(jù)
- vue?使用el-table循環(huán)輪播數(shù)據(jù)列表的實現(xiàn)
- vue實現(xiàn)動態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動畫
- vue如何從后臺獲取數(shù)據(jù)生成動態(tài)菜單列表
- Vue3 列表界面數(shù)據(jù)展示詳情
- vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法詳解
- vue2.0實現(xiàn)列表數(shù)據(jù)增加和刪除
- Vue如何獲取數(shù)據(jù)列表展示
- vue input輸入框關鍵字篩選檢索列表數(shù)據(jù)展示
- vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實現(xiàn)步驟
相關文章
electron-vue?運行報錯?Object.fromEntries?is?not?a?function
Object.fromEntries()?是?ECMAScript?2019?新增的一個靜態(tài)方法,用于將鍵值對列表(如數(shù)組)轉換為對象,如果在當前環(huán)境中不支持該方法,可以使用?polyfill?來提供類似功能,接下來通過本文介紹electron-vue?運行報錯?Object.fromEntries?is?not?a?function的解決方案2023-05-05
詳解Vue.js搭建路由報錯 router.map is not a function
這篇文章主要介紹了詳解Vue.js搭建路由報錯 router.map is not a function,非常具有實用價值,需要的朋友可以參考下2017-06-06

