手把手教你实现一个简易的Vue组件在线编辑器

开发
vue-cli使用过vue的我想大家都知道,那么xxx.vue组件是怎么运行的呢?怎么把template,script,style渲染到页面上的呢?今天我们手动写了个简易的Vue组件在线编辑器玩一玩。

 [[357690]]

vue-cli使用过vue的我想大家都知道,那么xxx.vue组件是怎么运行的呢?怎么把template,script,style渲染到页面上的呢?今天我们手动写了个简易的Vue组件在线编辑器玩一玩。

话不多说先看一下效果


准备工作

  1. 安装vuejs
  2. 新建xxx.html
  3. 新建xxx.css

编写页面

  1. <div id="app"
  2.        <textarea name="" id="" cols="30" rows="30" v-model="content" autofocus placeholder="请输入vue模板"></textarea> 
  3.        <div class="btn-center"
  4.            <button @click="run">运行代码</button> 
  5.            <button @click="reset">清除</button> 
  6.        </div> 
  7.    </div> 
  8.    <div id="result"></div> 
  9.    <script src="./node_modules/vue/dist/vue.js"></script> 
textarea 元素为vue组件代码的编写部分,button为按钮区域
  1. textarea { 
  2.             display: block; 
  3.             width: 100%; 
  4.             min-height: 100px; 
  5.             max-height: 500px; 
  6.             padding: 8px; 
  7.             resize: auto; 
  8.  } 
  9.  
  10.  button { 
  11.             margin-top: 8px; 
  12.             display: inline-block; 
  13.             padding: 5px 16px; 
  14.             font-size: 14px; 
  15.             font-weight: 500; 
  16.             line-height: 20px; 
  17.             white-space: nowrap; 
  18.             vertical-align: middle; 
  19.             cursor: pointer; 
  20.             -webkit-user-select: none; 
  21.             -moz-user-select: none; 
  22.             -ms-user-select: none; 
  23.             user-select: none; 
  24.             border: 1px solid; 
  25.             border-radius: 6px; 
  26.             -webkit-appearance: none; 
  27.             -moz-appearance: none; 
  28.             appearance: none; 
  29.  .btn-center{ 
  30.            text-align: center; 
  31.  } 

思路分解

在xxx.vue中,我们写组件通常遵循一下模板

  1. <template> 
  2.      
  3. </template> 
  4. <script> 
  5. export default { 
  6.      
  7. </script> 
  8. <style> 
  9.  
  10. </style> 

我们想到的是在拿到输入的内容之后,我们希望获取都tempalte,script,style中的内容,然后通过Vue.extend( options )方法挂载到页面的元素上即可。

解析标签

我们需要拿到内容包括下图红圈外的部分


可以利用字符串的match方法获取到每一段的开始标签的下标,开始标签的长度以及结束标签的下标,然后通过slice方法截取获取到想要的内容。

  1. getSource(type){ 
  2.     const reg = new RegExp(`<${type}[^>]*>`); 
  3.     let content = this.content; 
  4.     let matches = content.match(reg); 
  5.     if(matches){ 
  6.       let start = content.indexOf(matches[0])+matches[0].length; 
  7.       let end = content.lastIndexOf(`</${type}`); 
  8.       return content.slice(start,end
  9.     } 
  10.   }, 

截取之后获取到的结果


转化函数


在vue官网中,data必须是一个函数,我们拿到的是一个字符串

  1. export default { 
  2.     data(){ 
  3.         return { 
  4.             msg:'hello world' 
  5.         } 
  6.     }, 
  7.     methods:{ 
  8.         run(){ 
  9.             console.log("你好"
  10.         } 
  11.     } 

如何把一个字符串转化为可执行函数,可以参考如何让一个字符串执行?

我们可以用new Function方法将字符串转化为可执行函数,我们需要的是

  1. data(){ 
  2.         return { 
  3.             msg:'hello world' 
  4.         } 
  5.     }, 
  6.     methods:{ 
  7.         run(){ 
  8.             console.log("你好"
  9.         } 
  10.     } 

利用字符串的replace方法将export default 替换成return得到


完成代码

  1. run:function(){ 
  2.  let template = this.getSource("template"); 
  3.  if(!template) return 
  4.  let script = this.getSource("script"); 
  5.  if(script){ 
  6.    script = script.replace(/export default/,"return"); 
  7.  } 
  8.  let obj = new Function(script)(); 
  9.  obj.template = template; 
  10.  let Profile = Vue.extend(obj); 
  11.  new Profile().$mount("#result"
  12. }, 

处理样式

通过正则解析拿到style样式之后,添加到head中即可

  1. let styleCss = this.getSource("style"); 
  2. let style = document.createElement("style"); 
  3. style.innerHTML = styleCss; 
  4. document.head.appendChild(style); 

总结

以上就是本文的全部内容了,只是简单地借助Vue.extend()方法实现的一个简单的Vue组件在线编辑器

  1. <div id="app"
  2.     <textarea name="" id="" cols="30" rows="30" v-model="content" autofocus placeholder="请输入vue模板"></textarea> 
  3.     <div class="btn-center"
  4.         <button @click="run">运行代码</button> 
  5.         <button @click="reset">清除</button> 
  6.     </div> 
  7. </div> 
  8. <div id="result"></div> 
  9. <script src="./node_modules/vue/dist/vue.js"></script> 
  10. <script> 
  11.     new Vue({ 
  12.         el: "#app"
  13.         data() { 
  14.             return { 
  15.                 content: "" 
  16.             } 
  17.         }, 
  18.         methods: { 
  19.             getSource(type) { 
  20.                 const reg = new RegExp(`<${type}[^>]*>`); 
  21.                 let content = this.content; 
  22.                 let matches = content.match(reg); 
  23.                 if (matches) { 
  24.                     let start = content.indexOf(matches[0]) + matches[0].length; 
  25.                     let end = content.lastIndexOf(`</${type}`); 
  26.                     console.log(content.slice(start, end)); 
  27.                     return content.slice(start, end
  28.                 } 
  29.             }, 
  30.             run: function () { 
  31.                 let template = this.getSource("template"); 
  32.                 if (!template) return 
  33.                 let script = this.getSource("script"); 
  34.                 if (script) { 
  35.                     script = script.replace(/export default/, "return"); 
  36.                 } 
  37.                 let styleCss = this.getSource("style"); 
  38.                 let style = document.createElement("style"); 
  39.                 style.innerHTML = styleCss; 
  40.                 document.head.appendChild(style); 
  41.                 let obj = new Function(script)(); 
  42.                 obj.template = template; 
  43.                 let Profile = Vue.extend(obj); 
  44.                 new Profile().$mount("#result"
  45.             }, 
  46.             reset() { 
  47.                 this.content = '' 
  48.             } 
  49.         } 
  50.     }) 
  51. </script> 

 

责任编辑:姜华 来源: 前端简报
相关推荐

2022-09-22 12:38:46

antd form组件代码

2022-06-28 15:29:56

Python编程语言计时器

2020-12-02 12:29:24

Vue无限级联树形

2021-06-22 10:43:03

Webpack loader plugin

2022-08-26 08:01:38

DashWebJavaScrip

2019-08-26 09:25:23

RedisJavaLinux

2021-11-10 11:40:42

数据加解密算法

2023-04-26 12:46:43

DockerSpringKubernetes

2021-08-31 10:02:10

KubernetesLinux集群

2009-11-09 14:57:37

WCF上传文件

2011-01-06 10:39:25

.NET程序打包

2023-05-17 10:05:35

组件设计(Modal)组件

2024-02-06 10:04:49

Express框架repo

2018-11-22 09:17:21

消息推送系统

2016-11-01 09:46:04

2011-01-10 14:41:26

2011-05-03 15:59:00

黑盒打印机

2021-07-12 09:03:50

Python任务管理器cmd命令

2022-05-18 08:51:44

调用模板后端并行

2020-05-15 08:07:33

JWT登录单点
点赞
收藏

51CTO技术栈公众号