Vue作用域插槽實(shí)現(xiàn)方法及作用詳解
默認(rèn)插槽和具名插槽的概念比較好理解,這里主要以官方文檔的案例來講解一下作用域插槽。
首先是有一個(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.hdgsjgj.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àn)橹挥衏urrentUser可以訪問到user,出錯(cuò)的地方在這里:
然后,引入一個(gè)插槽prop:
<span> <slot :user='user'> {{ user.lastName }} </slot></span>
接著,需要給v-slot帶一個(gè)值來定義我們提供的插槽 prop 的名字:
<current-user> <template v-slot='wts'> {{ wts.user.firstName }} </template></current-user>
簡單的講作用域插槽就是讓插槽內(nèi)容能夠訪問子組件中才有的數(shù)據(jù),修改后便可以正常工作了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. phpstudy apache開啟ssi使用詳解2. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令3. Xml簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. 詳解瀏覽器的緩存機(jī)制6. xml中的空格之完全解說7. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器8. jsp文件下載功能實(shí)現(xiàn)代碼9. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)10. 如何在jsp界面中插入圖片
