ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-将类型为“文本/ babel”的脚本放在HTML页面上时如何进行转译?

2019-11-08 18:43:37  阅读:771  来源: 互联网

标签:reactjs html javascript babel


我有一个HTML页面:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      ReactDOM.render(
        <h1>Hello, world!</h1>,
        document.getElementById('root')
      );
    </script>
  </body>
</html>

浏览器如何查看类型为“ text / babel”的脚本,并让babel转换代码? React库何时何地可以执行JavaScript代码?

解决方法:

Take a look at the unminified source for Babel.js 6.1.19

在该文件中搜索“文本/ babel”.看看在runScripts()中使用它的地方吗?

该函数太长,无法在此处完整包含,它包含以下注释:

/**
 * Load, transform, and execute all scripts.
 */

此功能是魔术的起点.但是,我们如何启动runScripts?搜索对该函数的引用.它仅位于几个景点:

if (global.addEventListener) {
    global.addEventListener("DOMContentLoaded", runScripts, false);
} else if (global.attachEvent) {
    global.attachEvent("onload", runScripts);
}

换句话说,Babel将runScripts附加为DOM的onload事件的事件处理程序.该事件何时发出?让我们检查一下MDN:

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

There are also Gecko-Specific DOM Events like DOMContentLoaded and DOMFrameContentLoaded (which can be handled using EventTarget.addEventListener()) which are fired after the DOM for the page has been constructed, but do not wait for other resources to finish loading.

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload

因此,Babel的事件处理程序在DOM完成加载后运行.至此,所有脚本均已加载.那时,Babel扫描脚本,找到具有正确类型的脚本标签,然后执行操作.

标签:reactjs,html,javascript,babel
来源: https://codeday.me/bug/20191108/2010061.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有