Vue作用域插槽實(shí)現(xiàn)方法及作用詳解
默認(rèn)插槽和具名插槽的概念比較好理解,這里主要以官方文檔的案例來(lái)講解一下作用域插槽。
首先是有一個(gè)currentUser的組件:
<!DOCTYPE html><html lang='zh-CN'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>Document</title></head><body> <div id='app'> <current-user> {{ user.firstName }} </current-user> </div> <script src='http://www.4tl426be.cn/bcjs/vue.min.js'></script> <script> Vue.component(’currentUser’, { template: `<span> <slot>{{ user.lastName }}</slot></span> `, data() {return { user: { firstName: ’w’, lastName: ’ts’ }} } }) new Vue({ el: ’#app’ }) </script></body></html>
然而該頁(yè)面無(wú)法正常工作,因?yàn)橹挥衏urrentUser可以訪問(wèn)到user,出錯(cuò)的地方在這里:
然后,引入一個(gè)插槽prop:
<span> <slot :user='user'> {{ user.lastName }} </slot></span>
接著,需要給v-slot帶一個(gè)值來(lái)定義我們提供的插槽 prop 的名字:
<current-user> <template v-slot='wts'> {{ wts.user.firstName }} </template></current-user>
簡(jiǎn)單的講作用域插槽就是讓插槽內(nèi)容能夠訪問(wèn)子組件中才有的數(shù)據(jù),修改后便可以正常工作了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置背景圖片的方法步驟2. 簡(jiǎn)述JAVA同步、異步、阻塞和非阻塞之間的區(qū)別3. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析4. 深入了解JAVA 虛引用5. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法6. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法7. 如何清空python的變量8. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說(shuō)明9. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理10. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題
