首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现无区域角8项目

实现无区域角8项目
EN

Stack Overflow用户
提问于 2019-09-05 13:05:37
回答 1查看 1.1K关注 0票数 1

我正在将我的角项目迁移到角8,并遇到了以下两篇文章:

我现在有兴趣实现一个没有zonejs的项目。

我运行npm remove zone.js,然后删除polyfill.js中的import 'zone.js/dist/zone';,然后将, {ngZone: 'noop'}添加到main.ts中的bootstrapModule调用中。

当我的应用程序运行的时候。

当然,现在我应该手动添加需要检测更改的点,而这正是这些指南失败的地方。也许它们已经过时了(分别于2018年和2017年出版,仅在1-2年前)。

它们(以及您可以在.tick()上获得的其他谷歌点击)引用不存在的类或确实存在但没有.tick()的类。

对于角8-9,es6,ECMAScript,手动更改检测的类型实现,有什么真实的例子?

例如,现在我在登陆页上缺少了一个,它应该在令牌接收后重定向:

代码语言:javascript
复制
  ngOnInit() {
    this.loading = true;
    // reset login status
    if (this.window.navigator.userAgent != 'fakeAgent') {
      this.tryConnect();
      // Maybe tigger changedetect here?
    }
  }

  tryConnect(withLogout?: boolean) {
    this.error = '';
    this.loading = true;
    let uid = $('input#uid');
    if (!this.authenticationService.isOAuth && uid && uid.val()) {
      const data = {...};

      this.http.post(AppGlobal.API_URL + 'auth/tokens', data).subscribe(
        res => {
          // console.log(res);
          // Maybe tigger changedetect here?
          this.authenticationService.setToken(res.json());
          this.autologin();
        },
        err => {
          // console.log(err);
          this.authenticationService.setToken('unknown');
          this.autologin();
        }
      );
    }
  }

  autologin() {
    this.authenticationService.autologin().subscribe(
      result => {
        this.success = 'Connected';
        this.currentUser = this.authenticationService.getCurrentUser();
        let afterlogin = localStorage.getItem('afterlogin');
        let afterloginparams: any = JSON.parse(
          localStorage.getItem('afterloginparams')
        );
        if (!afterloginparams) {
          afterloginparams = {};
        }
        localStorage.removeItem('afterlogin');
        localStorage.removeItem('afterloginparams');
        if (afterlogin) {
          this.router.navigate([afterlogin], {queryParams: afterloginparams});
        } else {
          this.router.navigate(['/']);
        }
        // OR TRIGGER CHANGE DETECTION HERE ?
      },
      error => {
        let afterlogin = localStorage.getItem('afterlogin');
        let afterloginparams: any = JSON.parse(
          localStorage.getItem('afterloginparams')
        );
        if (!afterloginparams) {
          afterloginparams = {};
        }
        localStorage.removeItem('afterlogin');
        localStorage.removeItem('afterloginparams');
        if (afterlogin) {
          this.router.navigate([afterlogin], {queryParams: afterloginparams});
        } else {
          this.router.navigate(['/']);
        }
        this.finishedOnError('User not found');
      }
    );
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-05 13:09:16

如果您想要调用上的更改检测--整个角度视图树,那么您应该使用ApplicationRef实例,如下所示:

代码语言:javascript
复制
constructor(private appRef: ApplicationRef) {} 

someMethod() {
  this.appRef.tick();
}

但我建议你改用cdRef.detectChanges()

代码语言:javascript
复制
constructor(private cdRef: ChangeDetectorRef) {}    

someMethod() {
  this.cdRef.detectChanges();
}

这将调用当前组件及其所有子组件上的更改检测。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57806184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档