zoukankan      html  css  js  c++  java
  • vuejs关于函数式组件的探究

    所以,在控制台里app1.exist 或app2.exist都可以控制是否显示字母.

    <!DOCTYPE html>
    <html lang='zh'>
    <head>
      <title>TEST</title> 
    </head>
    
    <body>
    
    
      <div id="app1">
        <non-func :c_exist="exist"></non-func>
      </div>
      <div id="app2">
        <is-func :c_exist="exist"></is-func>
      </div>
    
    
    <script src="https://cdn.staticfile.org/vue/2.1.6/vue.min.js"></script>
    <script>
    
    var render1 = function (h) {
      var children = []
      if (this.c_exist) {
        children.push('Non-functional component')
      }
      return h('div', {}, children)  
    }
    Vue.component('non-func',{
      render :render1,
      props:['c_exist'],
    })
    var app1 = new Vue({
      el   : '#app1',
      data : {
        exist:true,
      },
    })
    
    // functional component
    var render2 = function (h, ctx) {
      console.log(ctx)
      var children = []
      if (ctx.data.attrs.c_exist) {  
        children.push('functional component')
      }
      return h('div', {}, children)  
    }
    Vue.component('is-func',{
      functional: true,
      render :render2,
    })
    
    var app2 = new Vue({
      el   : '#app2',
      data : {
        exist:true,
      },
    })
    
    </script>
    
    
    </body>
    </html>
    

    这样也可以:

    var render2 = function (h, ctx) {
      console.log(ctx)
      var children = []
      if (ctx.props.c_exist) {  
        children.push('functional component')
      }
      return h('div', {}, children)  
    }
    Vue.component('is-func',{
      functional: true,
      render :render2,
      props:['c_exist'],
    })
    
    var app2 = new Vue({
      el   : '#app2',
      data : {
        exist:true,
      },
    })
    
  • 相关阅读:
    函数传参-修改文本
    函数传参-商品计价
    嵌套选项卡自动播放
    仿淘宝自动播放菜单栏
    仿淘宝侧边栏菜单
    图片自动切换
    定时器应用-页面弹出广告
    转:Java面试题集(1-50)
    转:115个Java面试题和答案——终极列表(上)
    毕向东day01笔记--dos-jdk-jre-环境变量等
  • 原文地址:https://www.cnblogs.com/xiangnan/p/6754545.html
Copyright © 2011-2022 走看看