tnblog
首页
视频
资源
登录

Angular使用组件时遇到的一些错误整理

4141人阅读 2019/11/4 14:57 总访问:2580004 评论:0 收藏:0 手机
分类: 前端

先看看代码

export class SidenavComponent implements OnInit {

  private mediaMatcher: MediaQueryList = matchMedia(`(max-width: ${SMALL_WIDTH_BREAKPOINT}px)`);

  constructor(
    private router: Router,
    zone: NgZone) {
    this.mediaMatcher.addListener(mql => zone.run(() => this.mediaMatcher = mql));
  }

  @ViewChild(MatDrawer) drawer: MatDrawer;

  ngOnInit() {
    this.router.events.subscribe(() => {
      if (this.isScreenSmall()) {
        this.drawer.close();
      }
    });
  }

  isScreenSmall(): boolean {
    return this.mediaMatcher.matches;
  }

}

看着没什么错

实际根据版本的不同会有问题

(1)this未明确指明某对象

this.mediaMatcher.addListener(mql => zone.run(() => this.mediaMatcher = mql));

需改成

this.mediaMatcher.addListener(mql => {
      zone.run((x) =>x.mediaMatcher = mql)
    });

(2)这里也是更具版本的不同也会遇到问题

@ViewChild(MatDrawer) drawer: MatDrawer;

需改成

@ViewChild(MatDrawer,{ static:true }) drawer: MatDrawer;


后续会写一些Angluar多路由配置


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价
这一世以无限游戏为使命!
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术