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>

 

上一篇:2019.3.13 方法的重载


下一篇:Vue.js随笔二(新建路由+component+添加样式+变量的显示)