国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

requirejs + vue 項目搭建詳解

2019-11-19 16:19:26
字體:
來源:轉載
供稿:網友

以前都是支持 司徒正美 的,畢竟咱們也是跟著 司徒正美 一起走進了前端的世界。所以一般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], "http://'").replace(reg[1], "http:///n") + "<//template>');/n";    return "";  }).replace(regStyle, function (t, h) {    appendJs += "/tVue.appendCSS(/n'" + h.replace(reg[0], "http://'").trim().replace(reg[1], "http:///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);}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 钟山县| 韩城市| 洛浦县| 寿光市| 中宁县| 且末县| 盈江县| 盐源县| 舟山市| 会东县| 来凤县| 黎平县| 库尔勒市| 玉环县| 牙克石市| 衡阳市| 五指山市| 泸水县| 宿松县| 额济纳旗| 青河县| 濮阳县| 英吉沙县| 韶山市| 台东市| 梓潼县| 延长县| 平利县| 兴文县| 鹤峰县| 驻马店市| 宁国市| 古田县| 襄樊市| 东莞市| 祁门县| 通化市| 灵山县| 江口县| 海丰县| 布尔津县|