简体   繁体   English

拒绝位置后角组件无法正常工作

[英]Angular component not working after rejecting location

I'm using Angular for my web application. 我正在将Angular用于我的Web应用程序。 I'm facing the following problem. 我正面临以下问题。 In my parent module, I ask the user for location permissions, and in case that it accepts it goes to a route, in case it rejects, it goes to another route. 在我的父模块中,我向用户询问位置权限,如果接受则转到一条路由,如果拒绝则转到另一条路由。 Up to this point, everything is fine, the routing is working correctly. 至此,一切都很好,路由工作正常。 However, the problem comes with the module that must be loaded when the permission is denied. 但是,问题出在拒绝权限时必须加载的模块。

I had not noticed this before because, in case that the permissions had been denied before and the configuration was stored in the browser, the module and the component were loaded correctly, but the problem comes when it is the first time that the user is asked for the permission and he rejects it. 我以前没有注意到这一点,因为如果以前拒绝了权限并且配置已存储在浏览器中,则模块和组件已正确加载,但是问题出在第一次询问用户时获得许可,他拒绝了。 The routing is correct, but the component won't display as supposed, and no error shows in console. 路由正确,但是该组件将无法按预期显示,并且在控制台中未显示任何错误。

The html of the component is the following : 该组件的html如下:

<div class="no-location">
  <div class="container-fluid">
    <div class="row div-aligned-middle-middle message">
      {{ 'noLocation.title' | translate }}
    </div>
    <div class="row div-aligned-middle-middle">
      <div class="logo">
        <img width="140px" src="assets/img/logo">
      </div>
    </div>
    <div id="submit" hidden>
      <ngx-dropdown class="div-aligned-middle-middle" [optionList]="list" (selected)="selectedOption($event)"></ngx-dropdown>
      <div class="row btn-submit mx-auto div-aligned-middle-middle" (click)="submitOption()">
        <div>
          {{ 'noLocation.button' | translate }}
        </div>
      </div>
    </div>
  </div>
</div>

As you can see, the page has 3 main parts: 如您所见,该页面包含3个主要部分:

  1. A text 一条文字
  2. A image (logo) 图像(徽标)
  3. A dropdown and a button 下拉菜单和按钮

I'm using ngx-translate to translate the texts of the page. 我正在使用ngx-translate来翻译页面的文本。

When it is loading correctly, it shows everything, and in the dropdown, I can see the options that I fetch from my server via an HTTP call. 正确加载后,它会显示所有内容,并且在下拉列表中,我可以看到通过HTTP调用从服务器获取的选项。 However, when it fails, I can only see the logo. 但是,当它失败时,我只能看到徽标。

Here goes the TS: TS如下:

  constructor(private optionsService: OptionsService, private router: Router, private translate: TranslateService) {
  }

  ngOnInit() {
    setLang(this.translate);
    this.optionsService.fetchAllOptionsFromServer();
    this.optionsService.getOptionsObservable().subscribe(next => {
      document.getElementById('submit').hidden = false;
      this.list = next.map(optionDto => optionDto.name);
    });
  }

  selectedOption(str) {
    // do stuff
  }

  submitOption() {

    // do stuff
  }
}

As you can see, all I do is onInit, subscribe to the observable and call the server to retrieve the options to be shown in the dropdown. 如您所见,我要做的只是onInit,订阅可观察对象,然后调用服务器以检索要显示在下拉列表中的选项。 This call will update the observable which I have already subscribed to. 该调用将更新我已订阅的可观察对象。

I add the setLang function just in case somebody thinks the error can be related. 我添加了setLang函数,以防万一有人认为该错误可能与之相关。

export function setLang(translate: TranslateService, lang: string | null = null) {
  if (lang !== null) {
    translate.setDefaultLang(lang);
  } else {
    const locale = navigator.language.substr(0, 2);
    const availableLocales = ['es', 'en'];
    if (availableLocales.indexOf(locale) !== -1) {
      translate.setDefaultLang(locale);
    } else {
      translate.setDefaultLang('en');
    }
  }
}

通过在ngOnInit添加setTimeout()来解决此ngOnInit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM