【问题标题】:ng2-material TypeError: all_2.Media.hasMedia is not a functionng2-material TypeError: all_2.Media.hasMedia 不是函数
【发布时间】:2016-04-13 19:17:59
【问题描述】:

我正在尝试将 ng2-material 与 angular2 一起使用。

在尝试 Sidenav 组件时,我收到以下错误:

例外:TypeError: all_2.Media.hasMedia 不是 [hasMedia('gt-sm') 中的函数? 'side' : 'over' 在 AppComponent@17:32]

异常:错误:未捕获(承诺中):异常:TypeError:all_2.Media.hasMedia 不是 [hasMedia('gt-sm') 中的函数? 'side' : 'over' 在 AppComponent@17:32] 原始异常:TypeError:all_2.Media.hasMedia 不是函数 原始堆栈跟踪: TypeError: all_2.Media.hasMedia 不是函数 在 AppComponent.hasMedia (http://localhost:3000/app/app.component.js:67:40) 在 AbstractChangeDetector.ChangeDetector_AppComponent_0.detectChangesInRecordsInternal (viewFactory_AppComponent:114:31) 在 AbstractChangeDetector.detectChangesInRecords (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9609:14) 在 AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9592:12) 在 AbstractChangeDetector._detectChangesInViewChildren (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9671:14) 在 AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9596:12) 在 AbstractChangeDetector.detectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9581:12) 在 ChangeDetectorRef_.detectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:5252:16) 在http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12954:27 在 Array.forEach (本机) 错误上下文: [对象对象]

还有更多与all_2.Media.hasMedia相关的错误

这可能是导致它的代码

hasMedia(breakSize: string): boolean {
    return Media.hasMedia(breakSize);
}

我已经关注instructions on ng2-material site 并且已经开始使用 ng2-material。

我应该如何解决这个问题?

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    最初发布在 ng2-material 上的代码示例已更新为固定版本。这也让我感到困惑,我看到了 Shri 发布的相同错误。修复方法是将 Media 注入类并使用类本地版本:

    constructor(public sidenav: SidenavService,
                public media: Media) {
    }
    
    hasMedia(breakSize: string): boolean {
        return this.media.hasMedia(breakSize);
    }
    

    正如现在在 ng2-material 网站上所解释的:

    https://justindujardin.github.io/ng2-material/#/components/sidenav

    【讨论】:

    【解决方案2】:

    您正在使用示例代码。如您所见,它在某处调用了“hasMedia()”方法。这需要在您的组件中定义。 all_2 类似这样的内容仅指向您的上下文。

    【讨论】:

    • 我已经在我的组件中定义了 hasMedia() 并且正在被调用,因为我在方法中使用了 console.log 并进行了检查。现在我的函数还从库中的 Media 组件调用 hadMedia()。只需查看我在问题第一行中提到的演示站点即可。
    猜你喜欢
    • 1970-01-01
    • 2020-01-11
    • 2021-12-26
    • 2016-05-28
    • 2020-06-29
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多