Maison  >  Questions et réponses  >  le corps du texte

javascript - Comment introduire du HTML (y compris CSS/JS) en tant que module dans Vue

Le framework de code est vue+webpack+node

Je souhaite introduire un éditeur de plug-in markdown.md https://github.com/pandao/edi... en tant que module dans la page écrite par Vue

Mais editor.md ne peut pas être installé via npm

Code html du plugin editor.md :

<!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>

L'effet est le suivant :

Je veux mettre cela en tant que module dans la page implémentée par vue, mais je ne sais pas comment faire

怪我咯怪我咯2664 Il y a quelques jours1033

répondre à tous(2)je répondrai

  • PHP中文网

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

    Des plug-ins tiers similaires peuvent être organisés en un composant vue de cette manière :

    <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>

    Après l'instanciation, écoutez les méthodes fournies par l'instance ; puis $emitfournissez-les à l'utilisateur, puis fournissez quelques méthodes get pour obtenir les propriétés internes, etc.

    Pour une mise en œuvre spécifique, veuillez vous référer à vue-ueditorvue-echartssimilaires.

    Comment utiliser :

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

    répondre
    0
  • 三叔

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

    Mon implémentation spécifique :
    Mettez d'abord les dépendances requises par editor.md (disponibles sur github) dans le répertoire /static/editor.md/

    Code du composant MainEditor.vue :

    <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 doit être introduit séparément en html

    Adresse du composant vue (imitant vue-ueditor) : https://github.com/LaveyD/vue...

    répondre
    0
  • Annulerrépondre