vue學(xué)習(xí)筆記之作用域插槽實例分析
本文實例講述了vue學(xué)習(xí)筆記之作用域插槽。分享給大家供大家參考,具體如下:
<child></child>
Vue.component('child', {
data: function () {
return {
list: [1, 2, 3]
}
},
template: '<div>
<ul>
<li v-for="item of list">{{item}}</li>
</ul>
</div>'
})
那么,我們要想讓父組件每一次調(diào)用子組件時再定義顯示方式,也就是說,在子組件中定義好了v-for循環(huán)了list,具體怎么顯示,由父組件告訴我。那么在子組件中定義一個slot插槽,在父組件中添加一個作用域插槽【需要用template包裹】,在其內(nèi)寫顯示的樣式。
父組件需要得到子組件數(shù)據(jù)時,就需要template標(biāo)簽。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue中作用域插槽</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<child>
<template slot-scope="props">
<li>{{props.item}}</li><!--我想渲染成列表形式-->
</template>
</child>
</div>
</body>
</html>
<script>
Vue.component('child', {
data: function () {
return {
list: [1, 2, 3]
}
},
template: '<div><ul><slot v-for="item of list" :item="item">{{item}}</slot></ul></div>'
})
var vm = new Vue({
el: '#app'
})
</script>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
Vue data的數(shù)據(jù)響應(yīng)式到底是如何實現(xiàn)的
這篇文章主要介紹了Vue data的數(shù)據(jù)響應(yīng)式到底是如何實現(xiàn)的,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
el-table樹形數(shù)據(jù)量過大,導(dǎo)致頁面卡頓問題及解決
這篇文章主要介紹了el-table樹形數(shù)據(jù)量過大,導(dǎo)致頁面卡頓問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別
這篇文章主要介紹了詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

