kunyashaw博客主页 关注kunyashaw新博客 关于kunyashaw 转到底部

html调用commonjs规范的js

a.js

define(function(require, exports, module) {
    var test = function(){
        console.log("hello world")
    }
      
    //if using just this, Tern doesn't get it, but works with RequireJS
    module.exports = test;
  
    //this works with Tern already
    // return test;
  
  });

test.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button onclick="test()">test</button>
    <script>
        function test() {
            require(['./a.js'], function (sayHi) {
                console.log(sayHi);
            });
        }
    </script>
    <script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
</body>

</html>

 

posted @ 2019-05-20 19:38  kunyashaw  阅读(2027)  评论(0编辑  收藏  举报
回到顶部