您好,登錄后才能下訂單哦!
React 組件化思想受到越來越多開發(fā)者的關(guān)注,組件化思想幫助開發(fā)者將頁面解耦成一個一個組件,代碼更加模塊化, 更易擴展。而目前流行的后端模板引擎如 ejs, swig, jade, art 共同的問題是:
針對以上痛點,筆者基于 React 造出了 noox 這樣一個工具,專注于后端模板的解析,讓模板解析更加簡單,易用。
使用方法
安裝
npm install noox
簡單的 demo
模板代碼
首先創(chuàng)建組件目錄和增加模板文件
mkdir components && cd components vi Head.jsx
Head.jsx 內(nèi)容如下:
<head> <title>{title}</title> <meta name="description" content={props.description} /> <link rel="stylesheet" href="./css/style.css" rel="external nofollow" rel="external nofollow" /> </head>
Node.js Code
const noox = require('noox'); const nx = new noox(path.resolve(__dirname, './components'), {title: 'noox'}); let output = nx.render('Head', {description: 'hello, noox.'})
輸出
<head> <title>noox</title> <meta name="description" content="hello, noox." /> <link rel="stylesheet" href="./css/style.css" rel="external nofollow" rel="external nofollow" /> </head>
原理
Noox 在 React 的 Jsx 的基礎(chǔ)上,簡化了組件引用和創(chuàng)建,假設(shè)創(chuàng)建一個目錄結(jié)構(gòu)如下:
components/ Header.jsx Body.jsx Layout.jsx
運行如下 nodejs 的代碼:
nx = new noox(path.resolve(__dirname, './components'))
將會創(chuàng)建三個組件:
然后通過 nx.render 渲染
nx.render('Body', props)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。