首頁  >  文章  >  web前端  >  jquery載入單檔vue元件方法分享

jquery載入單檔vue元件方法分享

小云云
小云云原創
2018-01-06 11:31:491656瀏覽

本文主要介紹了jquery載入單檔案vue元件的方法,具有一定的參考價值,有興趣的小夥伴們可以參考一下,希望能幫助到大家。


/**注册组件 */
  function registerComponent(name){
    dm[name] = {};
    Vue.component(name + '-component', function(resolve, reject){
      $.get('./modules/' + name + '.vue').then(function(rv){
        var temp = rv.match(/<template[^>]*>([\s\S]*?)<\/template>/)[1].replace(/(^\s+)|\n/g, &#39;&#39;),
          script = rv.match(/<script[^>]*>([\s\S]*?)<\/script>/)[1].replace(/(^\s+)|\n/g, &#39;&#39;);
        script = (new Function(&#39;return &#39; + script))();
        script.template = temp;
        script.props || (script.props=["param"]);
        script.data || (script.data=function(){ return JSON.parse(JSON.stringify(this.param))});
        
        resolve(script);
      }).catch(function(err){
        console.error(err);
        resolve({
          template: &#39;<p style="text-align:center; line-height:5em;">NOT FOUND</p>&#39;
        });
      });
    });
  }

相關推薦:

關於Vue元件實作tips的總結

實例詳解基於vue組件實作猜數字遊戲

詳解vue組件父子間通訊

以上是jquery載入單檔vue元件方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn