0

我有一个导航应用程序。在页面控制中存在相应的 js 文件。我想访问项目 js 文件夹中的外部 js 文件。我已经指定

commonFunctions.js

WinJS.Namespace.define(

'commonFunctions', {

        authenticate: authenticate

    });

现在在访问 PageControl 的 js (page2.js) 文件中的方法时

commonFunctions.authenticate();

它给出了一个错误:

0x800a1391 - JavaScript 运行时错误:“commonFunctions”未定义

如果有这个异常的处理程序,程序可以安全地继续。

这是正确的方法吗?还是我错过了什么?

4

1 回答 1

1

我相信您遇到的问题是您没有在正在加载的页面中引用脚本文件,或者您没有将commonFunctions对象包装在函数中。

我试过这个:script.js

(function () {
    WinJS.Namespace.define("CommonObject", {
        auth: function () {
            return true;
        }
    });
}());

默认.js

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
                var test = CommonObject.auth(); // Comes out true and no exceptions
                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

默认.html

<script src="/js/script.js"></script>
于 2012-10-10T08:35:27.167 回答