zoukankan      html  css  js  c++  java
  • 插槽

    插槽的使用

    <!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>
      <script src="./../../../vue.min.js"></script>
    </head>
    <body>
      <div id="app">
        <cpn><span>www</span></cpn>
        <cpn><i>我是斜体</i></cpn>
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
      </div>
      <template id="cpn">
        <div> 
          <h2>标题1</h2>
          <span>玩压缩</span>
          <slot><button>按钮</button></slot>
        </div>
      </template>
      <script>
        const cpn=Vue.extend({
          template:"#cpn"
        })
      new Vue({
        el:"#app",
        components:{
          cpn:cpn
        }
      })
      </script>
    </body>
    </html>

    具名插槽

    <!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">
        <cpn><span slot="left"></span></cpn>
        <cpn><span slot="center"></span></cpn>
        <cpn><span slot="right"></span></cpn>
        <cpn></cpn>
      </div>
    <template id="cpn">
      <div>
        <slot><span name="left">左边</span></slot>
        <slot><span name="center">中间</span></slot>
        <slot><span name="right">右边</span></slot>
        <slot><button>按钮</button></slot>
      </div>
    </template>
    
      <script src="./../../../vue.min.js"></script>
      <script>
        const cpn=Vue.extend({
          template:"#cpn"
        })
      const app=new Vue({
        el:"#app",
        components:{
          cpn:cpn
        }
      })
      </script>
    </body>
    </html>

    作用域插槽

    <!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>
      <script src="https://unpkg.com/vue/dist/vue.js"></script>
    </head>
    <body>
      <div id="app">
        <cpn>
          <template slot-scope="slot">
            <span v-for="item in slot.data">{{item}} - </span>
          </template>
        </cpn>
    
        <cpn>
            <template slot-scope="slot">
                <span v-for="item in slot.data">{{item}} * </span>
              </template>
        </cpn>
    
        <cpn></cpn>
    
        <cpn>
            <template slot-scope="slot">
                <span>{{slot.data.join(" - ")}}</span>
              </template>
        </cpn>
    
      </div>
    
    <template id="cpn">
     <div>
        <slot v-bind:data="language">
            <ul>
              <li v-for="item in language">{{item}}</li>
            </ul>
          </slot>
     </div>
    </template>
    
      <script>
      const app=new Vue({
        el:"#app",
        components:{
          cpn:{
            template:"#cpn",
          data(){
            return {
            language : ["java","c++","javascript"]
            }
          }
          }
        }
      })
      </script>
    </body>
    </html>

  • 相关阅读:
    webpack学习最基本的使用方式(一)
    exports default 和export的使用方式
    vue兄弟组件之间的传值方式
    vue组件之间的传值方式
    vue组件创建学习总结
    vue实例生命周期钩子
    this 的工作原理
    node版本升级参考
    ES6 promise学习
    弹性布局学习总结
  • 原文地址:https://www.cnblogs.com/wxy0715/p/12442757.html
Copyright © 2011-2022 走看看