使用RequireJS设置smooch-js

时间:2016-04-22 14:36:33

标签: javascript requirejs smooch

我跟踪Smooch instructions但没有成功。

这是我失败的最小代码:

<!doctype html><html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
    <script>
        require.config({
            "paths": {
                "smooch": "https://cdn.smooch.io/smooch.min"
            }
        });
        // Tried this
        require(["require", "smooch"], function(require){
            var Smooch = require("smooch");
            console.log(Smooch);  // → undefined
        });
        // Tried that
        require(["smooch"], function(Smooch){
            console.log(Smooch);  // → undefined
        });
    </script>
</head>
<body></body>
</html>

require给我一个undefined,所以我没有Smooch.init(...)。在Firefox和Chrome中都进行了测试。

我做错了吗?

1 个答案:

答案 0 :(得分:3)

你应该使用&#34; Smooch&#34;而不是&#34; smooch&#34;在你的需求电话中,如下:

<!doctype html><html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
    <script>
        require.config({
            "paths": {
                "Smooch": "https://cdn.smooch.io/smooch.min"
            }
        });
        // Tried this
        require(["require", "Smooch"], function(require){
            var Smooch = require("Smooch");
            console.log(Smooch);  // → Smooch object
        });
        // Tried that
        require(["Smooch"], function(Smooch){
            console.log(Smooch);  // → Smooch object
        });
    </script>
</head>
<body></body>
</html>

lib包装在UMD包装器中,并自定义其名称为&#34; Smooch&#34;。