目录设置
我们创建一个目录Scratch,在本机上的路径为D:\Code\Scratch
Closure Library安装
我们使用Git将这个包下载到本地,命令如下
git clone https://github.com/google/closure-library
下载完成后,我们进入目录,然后安装该包,命令如下:
cd closure-library
npm install
安装完成后的输出如下
完成后,我们在D:\Code\Scratch安装两个文件,分别为hello.html和hello.js
hello.js
goog.require('goog.dom'); goog.require('goog.dom.TagName'); function sayHi() { var newHeader = goog.dom.createDom(goog.dom.TagName.H1, {'style': 'background-color:#EEE'}, 'Hello world!'); goog.dom.appendChild(document.body, newHeader); }
hello.html
<html> <head> <script src="closure-library/closure/goog/base.js"></script> <script src="hello.js"></script> </head> <body onload="sayHi()"> </body> </html>
随后,我们进行测试,检验hello.html是否能顺利打开
注:笔者用默认浏览器直接开启hello.html无法查看到任何内容
笔者是用WebStorm,将路径设置为D:\Code\Scratch后,在IDE环境下运行hello.html,才能正常运行hello.html
输出如下: