当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 了解VUE的render函数的使用

了解VUE的render函数的使用

2017年12月12日  | 移动技术网IT编程  | 我要评论

vue 推荐在绝大多数情况下使用 template 来创建你的 html。然而在一些场景中,你真的需要 javascript 的完全编程的能力,这就是 render 函数,它比 template 更接近编译器。 在 html 层, 我们决定这样定义组件接口:通过传入不同的level 1-6 生成h1-h6标签,和使用slot生成内容

<div id="div1">
  <child :level="1">hello world!</child>
</div>
<script type="text/x-template" id="child-template">
 <h1 v-if="level === 1">
  <slot></slot>
 </h1>
 <h2 v-if="level === 2">
  <slot></slot>
 </h2>
 <h3 v-if="level === 3">
  <slot></slot>
 </h3>
 <h4 v-if="level === 4">
  <slot></slot>
 </h4>
 <h5 v-if="level === 5">
  <slot></slot>
 </h5>
 <h6 v-if="level === 6">
  <slot></slot>
 </h6>
 </script>

<script type="text/javascript">
  /**
   * 全局注册child组件,注意template如果值以 # 开始,则它用作选项符,将使用匹配元素的 innerhtml 作为模板。常用的技巧是用 <script type="x-template"> 包含模板,这样的好处是html不会渲染里面的内容
   * 这里使用template不是最好的选择,
   * 一、代码冗长 
   * 二、在不同的标题插入内容需要重复使用slot 
   * 三、由于组件必须有根元素,所以标题和内容被包裹在一个无用的div中,比如<div><h1>hello world</h1></div>
   */

  vue.component('child', {
   template: '#child-template',
   props: {
    level: {
     type: number,
     required: true
    }
   },
   data: function() {
    return {
     a: 1
    }
   }
  })

  new vue({
    el:"#div1"
  })
 </script>

我们尝试使用render函数实现上面的例子,注意使用render函数,template 选项将被忽略。 createelement接收3个参数:

第一个参数可以是html标签名,组件或者函数都可以;此参数是必须的;

第二个为数据对象{object}(可选);

第三个为子节点{string | array}(可选),多个子节点[createelement(tag1),createelement(tag2)]。

<div id="div1">
  <child :level="1">
   hello world!
  </child>
  <child :level="2">
   <!-- 将不会被显示 -->
   <span slot="footer">span</span>
   <p slot="header">header slot<span>span</span></p>
  </child>
 </div>

vue.component('child', {
   render: function(createelement) {
    console.log(this.$slots);
    return createelement(
     'h'+ this.level, // tagname标签名称
     {
      // 为每个h标签设置class
      'class': {
       foo: true,
       bar: false
      },
      // 最终被渲染为内联样式
      style: {
       color: 'red',
       fontsize: '14px'
      },
      // 其他的html属性
      attrs: {
       id: 'foo',
       'data-id': 'bar'
      },
      // dom属性
      domprops: {
       // innerhtml: 'from domprops',
      },
      // 事件监听器基于 "on"
      // 所以不再支持如 v-on:keyup.enter 修饰器
      on: {
       click: this.clickhandler
      },
      // ...
     },
     // 你可以从this.$slots获取vnodes列表中的静态内容
     // $slots.default用来访问组件的不具名slot
     // 当你可能需要具名slot的时候需要指定slot的name, this.$slots.header
     [this.$slots.default]
    )
   },
   template: '<div v-if="level===1"><slot></slot></div>', // 将被忽略
   props: {
    level: {
     type: number,
     required: true
    }
   },
   methods: {
    clickhandler: function() {
     console.log('clickhandler')
    }
   }
  })

  new vue({
    el:"#div1"
  })

我们现在可以完成这样的组件

<h1>
   <a name="hello-world" href="#hello-world" rel="external nofollow" >
    hello world!
   </a>
</h1>

// 递归函数获得helloworld文本
  function getchildrentextcontent(child) {
    return child.map(function(node) {
      return node.children? getchildrentextcontent(node.children) : node.text
    }).join('')
  }
  vue.component('child',{
    render: function(createelement) {
      var hello_world = getchildrentextcontent(this.$slots.default)
               .tolowercase()
               .replace(/\w+/g,'-')
               .replace(/^\-|\-$/g,'');
      return createelement(
        'h'+ this.level,
        {},
        [ // 创建一个a标签,设置属性,并设置a标签的子节点
          createelement('a',{
            attrs: {
              name: hello_world,
              href: '#' + hello_world
            }
          },this.$slots.default)
        ]
      )
    },
    props: {
      level: {
        type: number,
        required: true
      }
    }
  })
  new vue({
    el:"#div1"
  })

注意vnode的唯一性,这里两个vnode指向同一引用是错误的,如果要重复创建多个相同元素/组件,可以使用工厂函数实现

<div id="div1">
  <child :level="1">
   hello world!
  </child>
</div>

vue.component('child',{
  // render: function(createelement) {
  // var myparagraphvnode = createelement('p','hello')
  // return createelement('div',
  //   [myparagraphvnode, myparagraphvnode]
  // )
  // },
  render: function(createelement) {
    return createelement('div',
      array.apply(null, {length:20}).map(function() {
        return createelement('p','hello')
      })
    )
  },
  props: {
    level: {
      type: number,
      required: true
    }
  }
})
new vue({
  el:"#div1"
})

使用javascript代替模板功能,某些api要自己实现

①使用if/else代替v-if

②使用map代替v-for

vue.component('child',{
  render: function(createelement) {
    if (this.lists.length) {
      return createelement('ul',this.lists.map(function() {
        return createelement('li','hi')
      }))
    } else {
      return createelement('p','no lists')
    }
  },
  props: {
    level: {
      type: number,
      required: true
    }
  },
  data: function() {
    return {
      lists: [1,2,3]
    }
  }
})

// render函数中没有与v-model相应的api - 你必须自己来实现相应的逻辑:
vue.component('child-msg',{
  render: function(createelement) {
    var self = this;
    return createelement('div', [
        createelement('input',{
          'on': {
            input: function(event) {
              self.value = event.target.value;
            }
          }
        }),createelement('p',self.value)
      ])
  },
  props: {
    level: {
      type: number,
      required: true
    }
  },
  data: function() {
    return {
      value: ''
    }
  }
})
new vue({
  el:"#div1"
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网