最近找了几个模块化的方案,posthtml还不是很成熟,css module需要和react一起比较好用,于是尝试了下polymer。
polymer是基于web component规范的,hello-world-polymer可以让我们快速的熟悉polymer。
polymer模块html,css,js都是写一起的, hello-word.html 代码如下
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-world"> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-world', properties: { who: { type: String, value: 'World' } }});</script>
定义好模块后,只要在 index.html 文件引入模块,然后用
<!doctype html><html><head> <meta charset="utf-8"> <title><hello-world></title> <!-- Imports polyfill --> <script src="../webcomponentsjs/webcomponents-lite.min.js"></script> <!-- Imports custom element --> <link rel="import" href="build/hello-world.html"></head><body> <!-- Runs custom element --> <hello-world who="world"></hello-world></body></html>
多模块也是没问题的,我们新建一个 hello-module.html ,并且给她一点样式
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-module"> <style> p{ color: red; display: flex; } strong{ color: black; } </style> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-module', properties: { who: { type: String, value: 'Module' } }});</script>
然后在 index.html 引入
<!doctype html><html><head> <meta charset="utf-8"> <title><hello-world></title> <!-- Imports polyfill --> <script src="../webcomponentsjs/webcomponents-lite.min.js"></script> <!-- Imports custom element --> <link rel="import" href="build/hello-module.html"> <link rel="import" href="build/hello-world.html"></head><body> <!-- Runs custom element --> <hello-module who="module"></hello-module> <hello-world who="world"></hello-world></body></html>
浏览器显示是这样的,polymer已经帮我们加好命名空间,样式是不会相互影响的。
但是一些css3属性怎么办呢,我们还需要autoprefixer或者cssnext。需要三个插件支持,在命令行输入
npm i --save gulp-posthtml posthtml-postcss postcss-cssnext
然后修改 gulpfile.js 文件
var gulp = require('gulp'), postcssPlugins = [require('postcss-cssnext')({ browsers: ['last 10 versions'] })]gulp.task('html', function() { var posthtml = require('gulp-posthtml'); return gulp.src('modules/*.html') .pipe(posthtml([ require('posthtml-postcss')(postcssPlugins) ]/*, options */)) .pipe(gulp.dest('build/'));});gulp.task('watch', function() { gulp.watch("modules/**.html",["html"]);});gulp.task('default', ['html', 'watch']);
在命令行输入 gulp 就会实时帮我们编译了。生成的模块代码如下
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-module"> <style> p{ color: red; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; } strong{ color: black; } </style> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-module', properties: { who: { type: String, value: 'Module' } }});</script>
这样浏览器就支持了,测试了一下,polymer支持安卓4.1,如果测试没什么问题,就可以愉快的用上了。

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

本文解釋了HTML5&lt; time&gt;語義日期/時間表示的元素。 它強調了DateTime屬性對機器可讀性(ISO 8601格式)的重要性,並在人類可讀文本旁邊,增強Accessibilit

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。