av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

手把手教你實(shí)現(xiàn)一個(gè)簡(jiǎn)易的Vue組件在線編輯器

vue-cli使用過(guò)vue的我想大家都知道,那么xxx.vue組件是怎么運(yùn)行的呢?怎么把template,script,style渲染到頁(yè)面上的呢?今天我們手動(dòng)寫(xiě)了個(gè)簡(jiǎn)易的Vue組件在線編輯器玩一玩。

靈武網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司從2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

話不多說(shuō)先看一下效果

準(zhǔn)備工作

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

編寫(xiě)頁(yè)面

  
 
 
 
  1.  
  2.         
  3.         
  4.            運(yùn)行代碼 
  5.            清除 
  6.        
 
  •    
  •  
  •    
  •  
  •     
  • textarea 元素為vue組件代碼的編寫(xiě)部分,button為按鈕區(qū)域

     
     
     
     
    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中,我們寫(xiě)組件通常遵循一下模板

     
     
     
     
    1.  
    2.  
    3.  

    我們想到的是在拿到輸入的內(nèi)容之后,我們希望獲取都tempalte,script,style中的內(nèi)容,然后通過(guò)Vue.extend( options )方法掛載到頁(yè)面的元素上即可。

    解析標(biāo)簽

    我們需要拿到內(nèi)容包括下圖紅圈外的部分

    可以利用字符串的match方法獲取到每一段的開(kāi)始標(biāo)簽的下標(biāo),開(kāi)始標(biāo)簽的長(zhǎng)度以及結(jié)束標(biāo)簽的下標(biāo),然后通過(guò)slice方法截取獲取到想要的內(nèi)容。

     
     
     
     
    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(`
    8.       return content.slice(start,end) 
    9.     } 
    10.   }, 

    截取之后獲取到的結(jié)果

    轉(zhuǎn)化函數(shù)

    在vue官網(wǎng)中,data必須是一個(gè)函數(shù),我們拿到的是一個(gè)字符串

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

    如何把一個(gè)字符串轉(zhuǎn)化為可執(zhí)行函數(shù),可以參考如何讓一個(gè)字符串執(zhí)行?

    我們可以用new Function方法將字符串轉(zhuǎn)化為可執(zhí)行函數(shù),我們需要的是

     
     
     
     
    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. }, 

    處理樣式

    通過(guò)正則解析拿到style樣式之后,添加到head中即可

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

    總結(jié)

    以上就是本文的全部?jī)?nèi)容了,只是簡(jiǎn)單地借助Vue.extend()方法實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的Vue組件在線編輯器

      
     
     
     
    1.  
    2.      
    3.      
    4.         運(yùn)行代碼 
    5.         清除 
    6.     
     
  •  
  •  
  •  
  •  

  • 文章題目:手把手教你實(shí)現(xiàn)一個(gè)簡(jiǎn)易的Vue組件在線編輯器
    本文網(wǎng)址:http://uogjgqi.cn/article/cdjgjoo.html
    掃二維碼與項(xiàng)目經(jīng)理溝通

    我們?cè)谖⑿派?4小時(shí)期待你的聲音

    解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流

    其他資訊