使用 React 的網(wǎng)頁源碼,結(jié)構(gòu)大致如下。
<!DOCTYPE html>
<html>
<head>
<script src="https://atts.w3cschool.cn/attachments/image/cimg/script>
<script src="../build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
// ** Our code goes here! **
</script>
</body>
</html>
上面代碼有兩個(gè)地方需要注意。首先,最后一個(gè) script 標(biāo)簽的 type 屬性為 text/babel 。這是因?yàn)?React 獨(dú)有的 JSX 語法,跟 JavaScript 不兼容。凡是使用 JSX 的地方,都要加上 type="text/babel" 。
其次,上面代碼一共用了兩個(gè)庫: react.js 和 Browser.js ,它們必須首先加載。其中,Browser.js 的作用是將 JSX 語法轉(zhuǎn)為 JavaScript 語法。這一步很消耗時(shí)間,實(shí)際上線的時(shí)候,應(yīng)該將它放到服務(wù)器完成。
$ babel src --out-dir build
上面命令可以將 src 子目錄的 js 文件進(jìn)行語法轉(zhuǎn)換,轉(zhuǎn)碼后的文件全部放在 build 子目錄。
更多建議: