search

Home  >  Q&A  >  body text

javascript - How to introduce an html (including css/js) as a module in Vue

The code framework is vue webpack node

I want to introduce a markdown plug-in editor.md https://github.com/pandao/edi... as a module into the page written by Vue

But editor.md cannot be installed through npm

The html code of the editor.md plug-in:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Editor</title>
</head>
<body>
<p id="editormd">
  <textarea style="display:none;">### Hello Editor.md !</textarea>
</p>

<link rel="stylesheet" href="css/editormd.min.css"/>
<script src="js/jquery.min.js"></script>
<script src="js/zepto.min.js"></script>
<script src="js/editormd.min.js"></script>
<script type="text/javascript">
  /* eslint-disable */
  $(function () {
    editormd("editormd", {
      width: "98%",
      height: 730,
      path: "lib/", // Autoload modules mode, codemirror, marked... dependents libs path
      codeFold: true,
      saveHTMLToTextarea: true,
      searchReplace: true,
      htmlDecode: "style,script,iframe|on*",
      emoji: true,
      taskList: true,
      tocm: true,                  // Using [TOCM]
      tex: true,                   // 开启科学公式TeX语言支持,默认关闭
      flowChart: true,             // 开启流程图支持,默认关闭
      sequenceDiagram: true,       // 开启时序/序列图支持,默认关闭,
      imageUpload: true,
      imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
      imageUploadURL: "examples/php/upload.php",
      onload: function () {
        console.log('onload', this);
      }
    });
  });
</script>
</body>
</html>

The effect is as follows:

I want to put this as a module into the vue implementation page, but I don’t know how to do it

怪我咯怪我咯2746 days ago1122

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:31:24

    Similar third-party plug-ins can be organized into a vue component in this way:

    <template>
    <p id="id">
        <textarea></textarea>
    </p>
    </template>
    <script>
    import scriptjs from 'scriptjs'
    export default {
        props: {
            id: String
        },
        mounted() {
            // 获取依赖的资源 - 如果需要异步加载的话
            Promise.all([
                scriptjs('jquery.min.js'),
                scriptjs('editormd.min.js')
            ])
            .then(() => {
                // do your logic.
                // 实例化,绑定事件等操作
            })
        },
        destoryed() {
            // 解绑全局事件
            // 销毁实例
        },
        methods: {
            // 返回一些有用的函数
        }
    }
    </script>

    After instantiation, listen to the methods provided by the instance; then $emit to the user, and then provide some get methods to obtain internal properties, etc.

    For specific implementation, please refer to vue-ueditor, vue-echarts and similar ones.

    How to use:

    <editor-md id="editormd" @update="doUpdate"></editor-md>

    reply
    0
  • 三叔

    三叔2017-06-12 09:31:24

    My specific implementation:
    First put the dependencies required by editor.md (available on github) in the /static/editor.md/ directory

    MainEditor.vue component code:

    <template>
      <p id="editor-md" class="main-editor">
        <textarea></textarea>
      </p>
    </template>
    
    <script>
      import $script from 'scriptjs';
    
      export default {
        name: 'EditDocMainEditor',
        props: {
          editorPath: {
            type: String,
            default: '/static/editor.md/',
          },
          editorConfig: {
            type: Object,
            default() {
              return {
                width: '88%',
                height: 530,
                path: '/static/editor.md/lib/', // Autoload modules mode, codemirror, marked... dependents libs path
                codeFold: true,
                saveHTMLToTextarea: true,
                searchReplace: true,
                htmlDecode: 'style,script,iframe|on*',
                emoji: true,
                taskList: true,
                tocm: true,                  // Using [TOCM]
                tex: true,                   // 开启科学公式TeX语言支持,默认关闭
                flowChart: true,             // 开启流程图支持,默认关闭
                sequenceDiagram: true,       // 开启时序/序列图支持,默认关闭,
                imageUpload: true,
                imageFormats: ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'],
                imageUploadURL: 'examples/php/upload.php',
                onload: () => {
                  // eslint-disable-next-line
                  console.log('onload', this);
                },
              };
            },
          },
        },
        data() {
          return {
            instance: null,
          };
        },
        created() {
        },
        mounted() {
          // async loading js dependencies
          // editormd depdend on jquery and zepto
          $script([
            `${this.editorPath}js/jquery.min.js`,
            `${this.editorPath}js/zepto.min.js`,
          ], () => {
            $script(`${this.editorPath}js/editormd.min.js`, () => {
              this.initEditor();
            });
          });
        },
        beforeDestroy() {
        },
        methods: {
          initEditor() {
            // eslint-disable-next-line
            this.$nextTick((editorMD = window.editormd) => {
              if (editorMD) {
                // Vue 异步执行 DOM 更新,template 里面的 script 标签异步创建
                // 所以,只能在 nextTick 里面初始化 editor.md
                this.instance = editorMD('editor-md', this.editorConfig);
              }
            });
          },
        },
      };
    </script>
    
    <style lang="stylus" scoped>
      .main-editor
        width 100%
        height 100%
        margin-top 100px;
    </style>
    

    /static/editor.md/css/editormd.min.css needs to be introduced separately in html

    vue component address (imitating vue-ueditor): https://github.com/LaveyD/vue...

    reply
    0
  • Cancelreply