欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue學(xué)習(xí)筆記之作用域插槽實例分析

 更新時間:2020年02月01日 12:33:44   作者:Rachel~Liu  
這篇文章主要介紹了vue學(xué)習(xí)筆記之作用域插槽,結(jié)合實例形式分析了vue.js作用域插槽基本使用方法及操作注意事項,需要的朋友可以參考下

本文實例講述了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)文章

最新評論