美文网首页
form-generator读源码笔记

form-generator读源码笔记

作者: 林哥学前端 | 来源:发表于2021-11-10 14:57 被阅读0次

    form-generator地址:https://gitee.com/mrhj/form-generator
    1.在html中使用环境变量

    <script src="https://lib.baomitu.com/vue/2.6.11/vue<%= process.env.NODE_ENV === 'production' ? '.min' : ''%>.js"></script>
    

    2.vue等包通过cdn引入,在打包时去掉

      <script src="https://lib.baomitu.com/vue/2.6.11/vue<%= process.env.NODE_ENV === 'production' ? '.min' : ''%>.js"></script>
      <script src="https://lib.baomitu.com/vue-router/3.1.3/vue-router.min.js"></script>
      <script src="https://lib.baomitu.com/element-ui/2.13.2/index.js"></script>
    
      configureWebpack: {
        externals: {
          vue: 'Vue',
          'vue-router': 'VueRouter',
          'element-ui': 'ELEMENT'
        }
      },
    

    3.使用与vue-vue-element-admin同样的方式处理svg
    使用svg-sprite-loader

      chainWebpack(config) {
        // set svg-sprite-loader
        config.module
          .rule('svg')
          .exclude.add(resolve('src/icons'))
          .end()
        config.module
          .rule('icons')
          .test(/\.svg$/)
          .include.add(resolve('src/icons'))
          .end()
          .use('svg-sprite-loader')
          .loader('svg-sprite-loader')
          .options({
            symbolId: 'icon-[name]'
          })
          .end()
      }
    

    4.在处理拖拽事件时,在App.vue中添加

        // fix: firefox 下 拖拽 会新打卡一个选项卡
        // https://github.com/JakHuang/form-generator/issues/15
        document.body.ondrop = event => {
          event.preventDefault()
          event.stopPropagation()
        }
    

    5.使用el-scrollbar,里面放滚动内容
    6.使用vuedraggable处理拖拽
    7.使用输的find方法,返回第一个符合条件的项

    let target = inputComponents.find(item => item.__config__.tagIcon === tagIcon)
    

    8.每一个表单项要生成唯一的字段名
    idGlobal是自增的

    item.__vModel__ = `field${this.idGlobal}`
    

    9.首字母大写

    export function titleCase(str) {
      return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
    }
    

    10.短杠转驼峰

    export function camelCase(str) {
      return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
    }
    

    9.在浏览器中代码格式化
    https://www.npmjs.com/package/js-beautify
    10.在浏览器中编辑代码
    https://www.npmjs.com/package/monaco-editor
    11.加载一个远程script

    const callbacks = {}
    
    /**
     * 加载一个远程脚本
     * @param {String} src 一个远程脚本
     * @param {Function} callback 回调
     */
    function loadScript(src, callback) {
      const existingScript = document.getElementById(src)
      const cb = callback || (() => {})
      if (!existingScript) {
        callbacks[src] = []
        const $script = document.createElement('script')
        $script.src = src
        $script.id = src
        $script.async = 1
        document.body.appendChild($script)
        const onEnd = 'onload' in $script ? stdOnEnd.bind($script) : ieOnEnd.bind($script)
        onEnd($script)
      }
    
      callbacks[src].push(cb)
    
      function stdOnEnd(script) {
        script.onload = () => {
          this.onerror = this.onload = null
          callbacks[src].forEach(item => {
            item(null, script)
          })
          delete callbacks[src]
        }
        script.onerror = () => {
          this.onerror = this.onload = null
          cb(new Error(`Failed to load ${src}`), script)
        }
      }
    
      function ieOnEnd(script) {
        script.onreadystatechange = () => {
          if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
          this.onreadystatechange = null
          callbacks[src].forEach(item => {
            item(null, script)
          })
          delete callbacks[src]
        }
      }
    }
    
    /**
     * 顺序加载一组远程脚本
     * @param {Array} list 一组远程脚本
     * @param {Function} cb 回调
     */
    export function loadScriptQueue(list, cb) {
      const first = list.shift()
      list.length ? loadScript(first, () => loadScriptQueue(list, cb)) : loadScript(first, cb)
    }
    
    export default loadScript
    
    

    12.http-vue-loader直接加载.vue文件
    https://www.npmjs.com/package/http-vue-loader
    13.保存文件到本地file-saver
    https://www.npmjs.com/package/file-saver

    相关文章

      网友评论

          本文标题:form-generator读源码笔记

          本文链接:https://www.haomeiwen.com/subject/feyzgltx.html