小编dal*_*ows的帖子

Angular 8 - 在错误时处理 SSE 重新连接

我正在研究一个 Angular 8(带有 Electron 6 和 Ionic 4)项目,现在我们正在评估阶段,我们正在决定是否用 SSE(服务器发送的事件)或 Web 套接字替换轮询。我的部分工作是研究 SSE。

我创建了生成随机数的小型快递应用程序,并且一切正常。唯一让我烦恼的是在服务器错误时重新连接的正确方法。

我的实现是这样的:

  private createSseSource(): Observable<MessageEvent> {
    return Observable.create(observer => {
      this.eventSource = new EventSource(SSE_URL);
      this.eventSource.onmessage = (event) => {
        this.zone.run(() => observer.next(event));
      };

    this.eventSource.onopen = (event) => {
      console.log('connection open');
    };

    this.eventSource.onerror = (error) => {
      console.log('looks like the best thing to do is to do nothing');
      // this.zone.run(() => observer.error(error));
      // this.closeSseConnection();
      // this.reconnectOnError();
    };
  });
}
Run Code Online (Sandbox Code Playgroud)

我试图reconnectOnError()按照这个答案实现功能,但我无法让它工作。然后我放弃了这个reconnectOnError()功能,这似乎是一件更好的事情。不要尝试关闭和重新连接,也不要将错误传播到 observable。只需坐等,当服务器再次运行时,它会自动重新连接。 …

javascript server-sent-events typescript electron angular

7
推荐指数
1
解决办法
1508
查看次数

ESlint Angular - no-unused-vars 命中类型定义

我正在升级/重构一个 Angular 项目(到Angular 8、Electron 6、Ionic 4),我们决定从 TSLint 切换到 ESLint。

我设置了一些规则并且它们正在运行,但我无法摆脱no-unused-vars类型定义的警告。当我运行 linting 时,我会收到此警告OperatorFunctionObservable这显然不是问题。

import { OperatorFunction, Observable, timer } from 'rxjs';
import { tap } from 'rxjs/operators';

export function executeDelayed<T>(fn: () => void, delayTime: number): OperatorFunction<T, T> {
  return function executeDelayedOperation(source: Observable<T>): Observable<T> {
   //...
  }
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js文件使用此配置

module.exports = {
    "env": {
        "browser": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:prettier/recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly" …
Run Code Online (Sandbox Code Playgroud)

javascript typescript eslint angular

6
推荐指数
1
解决办法
8315
查看次数

角度2手柄单击iframe内的链接

有没有办法吸引点击链接iframe

<iframe *ngIf="currentFrameUrl" id="contentFrame" [src]="currentFrameUrl | safe"></iframe>
Run Code Online (Sandbox Code Playgroud)

我的iframe组件仅包含订阅可观察变量的简单代码。

export class ContentFrameComponent implements OnInit {
    currentFrameUrl: string;
    currentMenuState: boolean;
    @ViewChild('contentFrame') iframe: ElementRef;

    constructor(private frameService: ContentFrameService) {        
      this.currentMenuState = true;      
    }

    ngOnInit(): void {
      this.frameService.providerCurrentUrl$.subscribe(data => this.currentFrameUrl = data);
      this.frameService.providerMenuState$.subscribe(data => this.currentMenuState = data);
    }

    ngAfterViewInit() {
      let doc = this.iframe.nativeElement.contentDocument ||this.iframe.nativeElement.contentWindow;
      if (typeof doc.addEventListener !== undefined) {
        console.log("inside if - addEventListener") // Is shown
        doc.addEventListener("click", this.iframeClickHandler, false)
      } else if (typeof doc.attachEvent !== undefined) {
        console.log("inside if - attachEvent …
Run Code Online (Sandbox Code Playgroud)

html iframe typescript angular

5
推荐指数
2
解决办法
4829
查看次数

Angular 6 路由器出口样式应用于其下方的元素

我正处于向角度应用程序添加路由器转换的阶段,我面临着奇怪的问题。for 的样式router-outlet应用于 DOM 层次结构中位于其下方的元素。

这是我的代码:

<main role="main">
  <!-- Header -->
  <app-header class="topnavbar-wrapper"></app-header>
  <!-- Page content-->
  <div [@fadeAnimation]="o.isActivated ? o.activatedRoute : ''">
    <router-outlet #o="outlet"></router-outlet>
  </div>
  <!-- Footer -->
  <app-footer></app-footer>
</main>
Run Code Online (Sandbox Code Playgroud)

造型:

main {
  box-sizing: border-box;
  margin-top: 4.5rem;
  min-height: 92vh;
  padding-bottom: 4rem;
  /* Height of footer */
  position: relative;
}
router-outlet ~ * {
  position: absolute;
  height: 100%;
  width: 100%;
}
footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  color: #fff;
  background-color: $secondary;
}
Run Code Online (Sandbox Code Playgroud)

动画工作很顺利,我对此没有任何问题。但是路由器出口样式应用于其下方的元素,这使得页脚固定在底部,覆盖内容。如果我清除路由器出口元素的样式,则动画将无法正常工作。

有时候是这样的

正如您在屏幕上看到的,<app-overview>元素的router-outlet样式是导致问题的原因。我想这个解决方案在于正确的 …

html sass angular angular-animations

2
推荐指数
1
解决办法
3620
查看次数

Angular 6 - <a> href附加到基本URL

我有一个显示在表中的用户列表,每个用户都有显示的链接,可以导航到.

<div class="inline-icon-text">
  <small class="text-muted d-md-none mr-3">Link</small>
  <a [attr.href]="candidate.url" target="_blank" [title]="candidate.url">
    <i class="material-icons">open_in_new</i>
  </a>
</div>
Run Code Online (Sandbox Code Playgroud)

问题是,当我检查链接元素时,它指向正确的地址,但点击后它会附加到app base url.

<a _ngcontent-c15="" target="_blank" href="www.test.sk" title="www.test.sk">...</a>
Run Code Online (Sandbox Code Playgroud)

点击后,它会在地址为localhost:4200/www.test.sk的新标签页中打开

我错过了什么?

html angular6

2
推荐指数
1
解决办法
7099
查看次数

Angular - 如何在文本区域中设置插入符位置

我以为这很容易做到,但我意识到我不知道该怎么做。我有一个带有文本区域的对话框,可以在其中编辑多行文本。这个想法是,当您打开此对话框并且已经有一些文本时,将插入符号设置为文本开头。所以我猜它会是位置0

首先我尝试在HTML中设置它,但它不起作用

<textarea
  matInput
  #textArea
  id="editExperimentTitleInput"
  [placeholder]="'experiment.title.placeholder' | translate"
  [(ngModel)]="title"
  autocomplete="off"
  spellcheck="false"
  [selectionStart]="0"
  [selectionEnd]="0"
  rows="10"
></textarea>
Run Code Online (Sandbox Code Playgroud)

这也行不通

@ViewChild('textArea') _textArea: ElementRef;

ngAfterViewInit(): void {
  const textArea = this._textArea.nativeElement as HTMLTextAreaElement;
  textArea.setSelectionRange(0, 0);
  //textArea.focus() // Throws expresion changed after....
}
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

textarea typescript angular

2
推荐指数
1
解决办法
4844
查看次数