【问题标题】:Angular2(ES5) HashLocationstartergy errorAngular2(ES5) HashLocationstartergy 错误
【发布时间】:2016-05-18 09:15:21
【问题描述】:

我想使用 Hashlocationstartergy 在 Angular2 中使用 ES5 进行路由。我为 bootstratpping main.js 截取的代码如下:

(function(app) {
    document.addEventListener('DOMContentLoaded', function() {

        ng.platform.browser.bootstrap(app.AppComponent, [
                ng.router.ROUTER_PROVIDERS, ,
                ng.core.provide(ng.common.LocationStrategy, {
                    useClass : ng.common.HashLocationStrategy
                }) ]);
    });

})(window.app || (window.app = {}));

运行时出现以下错误:

uncaught TypeError: Cannot read property 'getOptional' of undefined on application_ref.ts:289

注意:我在 html 文件中添加了 angular2-all.umd.dev.js、Rx.umd.js、shims_for_IE.js、angular2-polyfills.js 脚本。

【问题讨论】:

    标签: javascript angular angular2-routing


    【解决方案1】:

    您的问题在这里:

     ng.platform.browser.bootstrap(app.AppComponent, [
        ng.router.ROUTER_PROVIDERS, , <== undefined array item
    

    【讨论】:

    • 谢谢!实际问题是 LocationStrategy 和 HashLocationStrategy 在 ng.router 下,所以实际代码应该是 ng.platform.browser.bootstrap(app.AppComponent, [ ng.router.ROUTER_PROVIDERS, ng.core.provide(ng.router.LocationStrategy, { useClass : ng.router.HashLocationStrategy }) ]);
    • 嗨 chikka.anddev,你知道哪个版本适用于 ng.router.HashLocationStrategy 吗?我似乎无法让它在 beta17 (cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/…) 中工作。谢谢。
    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多