riot.tag2源码解析

360 阅读1分钟

介绍

这个函数其实很简单,就不贴执行步骤了,就看下他的源码及输入输出:

  /**
   * Create a new riot tag implementation (for use by the compiler)
   * @param   { String }   name - name/id of the new riot tag
   * @param   { String }   tmpl - tag template
   * @param   { String }   css - custom tag css
   * @param   { String }   attrs - root tag attributes
   * @param   { Function } fn - user function
   * @returns { String } name/id of the tag just created
   */
  function tag2(name, tmpl, css, attrs, fn) {
    // 把css字符串存起来,最终append到style标签中
    if (css) { styleManager.add(css, name); }

    __TAG_IMPL[name] = { name: name, tmpl: tmpl, attrs: attrs, fn: fn };

    return name
  }

他的作用其实就是把模板上的html/js/attrs/fn分别存到一个对象中,然后存到__TAG_IMPL中。

至于CSS,就都被添加到styleManager中处理了,最终会被插入到<head><style>标签中。