小编fun*_*kid的帖子

vscode 有没有可以识别 javascript 中 jinja 变量的插件?

类似的问题是这样的: Javascript is not recognize a Flask variable

当我在 vscode 中输入这样的内容时,

<script type="text/javascript">
    var x ={{ data }};
</script>
Run Code Online (Sandbox Code Playgroud)

它总是会像这样重新格式化

<script type="text/javascript">
    var x = {
        {
            data
        }
    };
</script>
Run Code Online (Sandbox Code Playgroud)

不再是 Jinja 变量,并且不能真正在 js 中工作。
那么,vscode中有没有这样的插件可以解决这个问题呢?

javascript jinja2 visual-studio-code

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

如何在 Angular 中获取 DOM 节点属性

我正在使用这个答案将 HTML 字符串转换为 Angular 中的 DOM 元素。

问题是我无法获取 的属性NodegetAttribute()无法使用,因为 typescript 会抱怨No property getAttribute on Node.

代码如下(简化)。

import { AfterViewInit, Renderer2 } from '@angular/core';

export class MockComponent implements AfterViewInit {
  constructor(private renderer: Renderer2) {}

  private mockString = '<a href="#test1">test 1</a>'

  ngAfterViewInit(): void {
    const template = this.renderer.createElement('template');
    template.innerHTML = this.mockString.trim();
    const anchorNodes: NodeList = template.content.querySelectorAll('a');
    const anchors: Node[] = Array.from(anchorNodes);
    for (const anchor of anchors) {
      // how to get attributes of anchor …
Run Code Online (Sandbox Code Playgroud)

javascript dom typescript angular

4
推荐指数
1
解决办法
9020
查看次数

重定向到 404 组件而不更改浏览器 URL,并使历史记录在 Angular 中正常运行

当我的后端向我发送404错误(URL 有效,只是因为找不到资源,例如http://localhost:4200/post/title-not-exist)时,我希望 Angular 重定向到我的NotFoundComponent,而不更改浏览器中的 URL。

\n\n

代码如下(简化):

\n\n
constructor(private router: Router, private location: Location) {}\n\nhandleError(err: HttpErrorResponse) {\n  switch (err.status) {\n    case 404:\n      url = this.router.routerState.snapshot.url;\n      // \'**\' is set in the app-routing.module.ts\n      // { path: \'**\', component: NotFoundComponent }\n      this.router.navigate([\'**\'], { replaceUrl: false });\n      // weird here too\n      // without the setTimeout(), replaceState will not work\n      setTimeout(() => {\n        this.location.replaceState(url);\n      });\n      break;\n\n    default:\n      break;\n  }\n  return throwError(err);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我可以重定向到NotFoundComponent并且 URL 没有更改,问题是:

\n\n …

javascript routes typescript angular

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

在 Angular 组件中获取 &lt;html&gt; 标签

<body>并且<head>可以通过注入在 Angular 组件中获取标签DOCUMENT,如下所示:

import { DOCUMENT } from '@angular/common';
import { Inject } from '@angular/core';

export class TestComponent {
  constructor(
    @Inject(DOCUMENT) private document: Document
  )

  // get <head>
  // this.document.head

  // get <body>
  // this.document.body
}
Run Code Online (Sandbox Code Playgroud)

但是可以<html>在 Angular 组件中获取标签吗?

javascript typescript angular

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