我已經創建了一個插件。您可以將它用於Durandal 2.0。
define(['jquery'], function ($) {
return {
loadCss : function (fileName) {
var cssTag = document.createElement("link")
cssTag.setAttribute("rel", "stylesheet")
cssTag.setAttribute("type", "text/css")
cssTag.setAttribute("href", fileName)
cssTag.setAttribute("class", "__dynamicCss")
document.getElementsByTagName("head")[0].appendChild(cssTag)
},
removeModuleCss: function() {
$(".__dynamicCss").remove();
}
};
});
你可以在你的viewmodel中使用它。
define(['plugins/cssLoader'], function (cssLoader) {
var ctor = function() {
this.compositionComplete = function() {
cssLoader.loadCss("sample.css");
cssLoader.loadCss("sample2.css");
};
this.deactivate = function() {
cssLoader.removeModuleCss();
}
};
return ctor;
});
除了require.js css加載器插件,有沒有人使用任何其他方法?我通常會將我的所有CSS都縮小到一個基本文件中,並將其包含在我的index.html中,但是隻有我的CSS才能加載模塊會更方便。 – Rodiwa