当前位置: 移动技术网 > IT编程>脚本编程>vue.js > requirejs + vue 项目搭建详解

requirejs + vue 项目搭建详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

以前都是支持 司徒正美 的,毕竟咱们也是跟着 司徒正美 一起走进了前端的世界。所以一般mvvm都是用avalon的,当然也是考虑到项目需要支持ie6,7,8的考虑。当然在用的时候也有一些小坑和bug,都处理了。今年正美正好升级avalon2.0,加入虚拟dom的时候,不稳定了,就考试寻找其他的mvvm框架。

看了比较流行的一些框架,最后选择了vue。选择他的原因是 文档比较全,而且还有中文的(你懂的),生态圈比较好,有vux, vue-loader, vue-router ,组件化设计的也很好。

项目搭建的时候主要还是通过requirejs进行js模块加载(还没接触webpack,以前都是avalon+requirejs,习惯性思维了,下面就写写心路历程吧)

方案1,考虑能不能通过写个 requirejs 插件进行vue组件文件的加载

失败,主要是vue组件文件有template,script,style标签标签,主要通过requirejs,读取vue文件string文件进行正则分析在转换js, 有点舍近求远的方法,而且这种方式只能同域名ajax请求

方案2,通过编写gulp插件,将vue文件转换为可以通过requirejs加载的js文件。

这个方案是可行的,只是template,script,style信息,需要通过正则匹配,在动态载入到当前文档中,只是有些公用方法频繁调用。

所以加入了vuecomment文件,把动态加入的方法整理在一起

define(['vue'], function (vue) {
  vue.appendhtml = function (text) {
    document.body.insertadjacenthtml('beforeend', text);
  };
  var style;
  var doc = document;
  vue.appendcss = function (text) {
    text = text + " ";
    if (!style) {
      var head = doc.getelementsbytagname("head")[0]; 
      var elms = head.getelementsbytagname("style"); 
      if (elms.length == 0) {
        if (doc.createstylesheet) {
          doc.createstylesheet(); 
        } else { 
          var tmp = doc.createelement('style');
          tmp.setattribute("type", "text/css"); 
          head.appendchild(tmp); 
        }
        elms[0].setattribute("media", "screen"); 
      } 
      style = elms[0];
    }
    if (style.stylesheet) {
      style.stylesheet.csstext += text; 
    } else if(doc.getboxobjectfor) { 
      style.innerhtml += text;
    } else { 
      style.appendchild(doc.createtextnode(text)) 
    } 
  };
  
});

gulp-vue nodejs主要代码如下,通过文件名,来确定组件名字

var through = require('through2');
var gutil = require('gulp-util');

var regtpl = /<template>([\s\s]+?)<\/template>/;
var regstyle = /<style>([\s\s]+?)<\/style>/; 
var regjs = /<script>([\s\s]+?)<\/script>/; 
var reg = [/'/g, /\n/g, /([^\\]+)\.vue$/];

var vuewrite = function (file, str) {
  var match = file.path.match(reg[2]);
  var id = "vue-tpl-" + match[1];
  var appendjs = "";
  var res = "";
  str = str.replace(regtpl, function (t, h) {
    appendjs += "\tvue.appendhtml(\n'<template id=\"" + id + "\">" + h.replace(reg[0], "\\'").replace(reg[1], "\\\n") + "<\/template>');\n";
    return "";
  }).replace(regstyle, function (t, h) {
    appendjs += "\tvue.appendcss(\n'" + h.replace(reg[0], "\\'").trim().replace(reg[1], "\\\n") + "');\n"
    return "";
  }).replace(regjs, function (t, h) {
    res = "define(function (require) {\n\trequire('vuecommon'); \n\tvar vue = require('vue');\n\tvar exports;\n" + appendjs + h + ";\n\texports.template = '#" + id + "';\n\texports = vue.extend(exports);\n\tvue.component('" + match[1] + "', exports);\n\treturn exports;\n});"
    return ;
  })
  return res;
};

module.exports = function(opt){

 function run (file, encoding, callback) {
  if (file.isnull()) {
   return callback(null, file);
  }

  if (file.isstream()) {
   return callback(new gutil.pluginerror('gulp-vue', 'doesn\'t support streams'));
  }

  file.contents = new buffer(vuewrite(file, file.contents.tostring()));
  file.path = file.path + '.js';
  callback(null, file);
 }

 return through.obj(run);
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网