• vue render slot


    slot    内容分发  插槽内可以包含任何模板代码(html 组件)     

    父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。

    1. html 组件

    Vue.component('alert-box', {
      template: `
        <div class="demo-alert-box">
          <strong>Error!</strong>
          <slot></slot>
        </div>
      `
    })
    
    Vue.component('hello', {
      template: `
        <div class="hello">
          <a>link</a>
          hello
      
        </div>
      `
    })
    
    Vue.prototype.isActiveId = 1;
    
    var myname = 'wj'
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      // router,
      // store,
      // components: { App },
      // template: '<App/>'
    
      // render:function(h){
      //   return h('hello')
      // }
    
    
      template:`<alert-box>
      <hello />
      <p>my power</p>
      Something bad happened.
    </alert-box>`
    
    
    
    
    })
    View Code

    2. 后背内容

    <button type="submit">
    <slot>Submit</slot>
    </button>

    3. 具名插槽

    Vue.component('hello',{
      template:`
      <div class="container">
        <header>
          <slot name="header"></slot>
          <slot name="hhee"></slot>
          <slot name="header-wrong"></slot>
        </header>
        <main>
          <slot></slot>
        </main>
        <footer>
          <slot name="footer"></slot>
        </footer>
      </div>
    
      `
    })
    
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      // router,
      // store,
      // components: { App },
      // template: '<App/>'
    
      template:`
      <hello>
    
      <template slot="header">
        <h1>Here might be a page title</h1>
      </template>
    
    
      <h2 slot="hhee">hhee</h2>
    
      <template v-slot="header-wrong">
        <h1>past,wrong way</h1>
      </template>
    
    
      <p>A paragraph for the main content.</p>
      <p>And another one.</p>
    
      <template slot="footer">
        <p>Here's some contact info</p>
      </template>
    
      </hello>
      `
    
    })
    View Code

    render

    1 替代 template 多条件判断的普通用法

    <!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">
            <my-component :list="list"></my-component>
        </div>
        <script src="vue.js"></script>
        <script>
            Vue.component('my-component', {
                props: {
                    list: {
                        type: Array,
                        default: () => []
                    }
                },
                render(createElement) {
                    if (this.list.length) {
                        return createElement('ul', this.list.map(item => createElement('li', item)))
                    } else {
                        return createElement('p', 'Empty list')
                    }
                }
            })
            new Vue({
                el: '#app',
                data: {
                    list: ['html', 'css', 'javascript']
                }
            })
        </script>
    </body>
    </html>
    View Code

    2 自定义指令、多组元素

    Vue.component('my-component', {
        data() {
            return {
                message: ''
            }
        },
        render(createElement) {
            return createElement(
                'div',
                [
                    createElement(
                        'input',
                        {
                            on: {
                                input: e => this.message = e.target.value
                            }
                        }
                    ),
                    createElement('p', this.message)
                ]
            )
        }
    })
    View Code
  • 相关阅读:
    webpack + react 前端工程化实践和暂不极致优化
    图解Javascript——作用域、作用域链、闭包
    图解Javascript——变量对象和活动对象
    图解Javascript——执行上下文
    简单实用的进度条、时间轴
    Nginx配置文件nginx.conf中文详解(转)
    负载均衡——nginx理论
    JavaScript的闭包是什么意思以及作用和应用场景
    巧用HTML5给按钮背景设计不同的动画
    利用js的for循环实现一个简单的“九九乘法表”
  • 原文地址:https://www.cnblogs.com/justSmile2/p/12097695.html
Copyright © 2020-2023  润新知