国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Vue作用域插槽實現方法及作用詳解

瀏覽:2日期:2023-01-08 18:47:28

默認插槽和具名插槽的概念比較好理解,這里主要以官方文檔的案例來講解一下作用域插槽。

首先是有一個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.intensediesel.com/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>

然而該頁面無法正常工作,因為只有currentUser可以訪問到user,出錯的地方在這里:

Vue作用域插槽實現方法及作用詳解

然后,引入一個插槽prop:

<span> <slot :user='user'> {{ user.lastName }} </slot></span>

接著,需要給v-slot帶一個值來定義我們提供的插槽 prop 的名字:

<current-user> <template v-slot='wts'> {{ wts.user.firstName }} </template></current-user>

簡單的講作用域插槽就是讓插槽內容能夠訪問子組件中才有的數據,修改后便可以正常工作了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 中西区| 林口县| 五莲县| 岢岚县| 犍为县| 延庆县| 达孜县| 铜鼓县| 亚东县| 万源市| 渭南市| 姚安县| 读书| 金秀| 涞源县| 肃宁县| 宜良县| 上林县| 宝兴县| 通河县| 子长县| 上高县| 墨竹工卡县| 舒兰市| 三河市| 乐安县| 柞水县| 黎城县| 竹溪县| 屏边| 昆明市| 五台县| 萍乡市| 沙雅县| 缙云县| 宜都市| 安义县| 金华市| 孝感市| 巴南区| 许昌市|