1. 程式人生 > >Vue-插槽學習

Vue-插槽學習

ini 2個 html rop meta return prop 特性 itl

<!DOCTYPE html>
<html lang="en">
<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">
    <child1>
      <header slot=‘header‘>插槽header</header>
      <footer slot=‘footer‘>插槽footer</footer>
    </child1>
    <hr/>
    <!-- 當我們使用 <child2> 組件的時候,我們可以選擇為text定義一個不一樣的 <template> 作為替代方案,
    並且可以通過 slot
-scope 特性從子組件獲取數據: --> <child2> <template slot-scope="slotProps"> <p>第1個child2組件text是h1</p> <h1>{{slotProps.text}}</h1> </template> </child2> <child2> <template slot-scope="slotProps"> <p>第2個child2組件text是h2</p> <h2>{{slotProps.text}}</h2> </template> </child2> </div> </body> <script type="text/javascript" src="./vue.js"></script> <script> Vue.prototype.bus
=new Vue(); //具名插槽 Vue.component(‘child1‘,{ template:` <div> <slot name=‘header‘></slot> <p>child1內容</p> <slot name=‘footer‘></slot> </div> `, }) //作用域插槽 //使用場景:我們希望每個child2組件都有可復用數據text,但是渲染出第東西又不太一樣。 Vue.component(‘child2‘,{ data:
function(){ return{ text:‘子組件數據‘ } }, template:` <div> <p>child2內容</p> <slot :text=text></slot> </div> `, }) var app=new Vue({ el:‘#app‘, }) </script> </html>

Vue-插槽學習