jquery加载单文件vue组件的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

jquery加载单文件vue组件的方法

zh33gl   2020-05-15 我要评论

本文为大家分享了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, ''),
          script = rv.match(/<script[^>]*>([\s\S]*?)<\/script>/)[1].replace(/(^\s+)|\n/g, '');
        script = (new Function('return ' + 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: '<div style="text-align:center; line-height:5em;">NOT FOUND</div>'
        });
      });
    });
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们