小编Ton*_*Ngo的帖子

可观察的of of已过时。等价的是什么?

就我而言,我有一个访问令牌,并且如果该令牌存在,我将其作为可观察的字符串类型返回:

if (this.accessToken){
  return of(this.accessToken);
}
Run Code Online (Sandbox Code Playgroud)

由于最近的更新,我注意到of以下消息已弃用该消息:

of is deprecated: use scheduled instead 'scheduled([a, b, c], scheduler)' (deprecation)

新语法非常冗长,有人会知道scheduled相同简单语法的等效版本of吗?关键字名称使搜索有关它的信息变得困难。

谢谢!

javascript rxjs typescript angular

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

在 Angular 中,使用 Renderer 比使用 ElementRef 有什么优势?

我是 Angular 的新手,我试图使用两个选项创建我自己的指令:

  • 选项 1:使用 ElementRef 直接访问元素
  • 选项 2:使用 Renderer2

选项1:

import { Directive, ElementRef, OnInit } from '@angular/core';

@Directive({
    selector: '[appBasicHighlight]'
})
export class BasicHighlightDirective implements OnInit {
    constructor(private elementRef: ElementRef) {}

    ngOnInit() {
        this.elementRef.nativeElement.style.backgroundColor = 'green';
    }
}
Run Code Online (Sandbox Code Playgroud)

选项 2:

import { Directive, ElementRef, Renderer2, OnInit } from '@angular/core';

@Directive({
  selector: '[appBetterHighlight]'
})
export class BetterHighlightDirective {

  constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

  ngOnInit() {
    this.renderer.setStyle(this.elementRef.nativeElement, 'background-color', 'blue');
  }
}
Run Code Online (Sandbox Code Playgroud)

教程中的讲师说它更安全,并建议使用 Renderer 而不是直接访问,但没有明确解释原因。他的意思是使用 ElementRef 直接访问 DOM,但正如您从代码中看到的,Renderer …

node.js single-page-application typescript ecmascript-6 angular

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

Visual Studio Code:Angular 项目中出现“找不到‘tslib’”错误

基本上,我所做的就是启动一个新的 Angular 项目并将其加载到工作区中。由于此错误,VS Code 似乎无法执行任何 linting 或代码感知。它正在吐出以下错误数据:

{
    "resource": "/Users/<snip!>/angular-project/src/app/app.module.ts",
    "owner": "typescript",
    "code": "2354",
    "severity": 8,
    "message": "This syntax requires an imported helper but module 'tslib' cannot be found.",
    "source": "ts",
    "startLineNumber": 13,
    "startColumn": 1,
    "endLineNumber": 26,
    "endColumn": 3
}
Run Code Online (Sandbox Code Playgroud)

我在某处发现了一个旧错误,说moduleResolution必须tsconfig.json设置为node,但 Angular CLI 已经为我做到了这一点......

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": …
Run Code Online (Sandbox Code Playgroud)

node.js single-page-application typescript visual-studio-code angular

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

ngrx 效果在返回值后被多次调用

ngrx 效果在返回值后被多次调用。

loadMovies$: Observable<Action> = createEffect(() => {
    return this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
        .pipe(
          map(movies => {
             return new counterActions.IncrementCounter();
          }));
      }
    ));
  });
Run Code Online (Sandbox Code Playgroud)

javascript rxjs typescript ngrx angular

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

如何使用货币管道在组件中获取最多 2 个十进制数字的值?

我有一个像 54781.7622000 、 1123.11 这样的值。我希望这个值的格式为 $54781.76 , $1123.11 。

//import currency pipe
import { CurrencyPipe } from '@angular/common'

//initialize  in constructor
constructor(private cp: CurrencyPipe)

this.formatedOutputValue = this.cp.transform(parseInt(this.outputValue));
Run Code Online (Sandbox Code Playgroud)

我已经尝试过在这个值之后对额外的参数进行求和。

this.formatedOutputValue = this.cp.transform(parseInt(this.outputValue),1.2-2);
Run Code Online (Sandbox Code Playgroud)

但是不锻炼。

javascript typescript angular-pipe angular angular8

4
推荐指数
3
解决办法
8358
查看次数

如何在 React 中显示来自 django-rest-framework 的错误消息

我正在尝试使用 Django rest 框架和 react,redux 来实现用户注册表。我能够成功注册用户,但我在显示错误时遇到了问题,这些错误是由 Django 提供的,以防万一。

到目前为止我所做的

export const AUTH_START = 'AUTH_START';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_FAIL = 'AUTH_FAIL';
export const AUTH_LOGOUT = 'AUTH_LOGOUT';
Run Code Online (Sandbox Code Playgroud)

这是Reducer功能

const initialState = {
    token: null,
    error: null,
    loading: false
}

const authFail = (state, action) => {
    return updateObject(state, {
        error: action.error,
        loading: false
    });

}

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.AUTH_START:
            return authStart(state, action);
        case actionTypes.AUTH_SUCCESS:
            return authSuccess(state, action);
        case …
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-6 reactjs redux

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

在控制器之外获取当前用户

在 ASP.NET Core 2.2 控制器上,我有以下内容:

var principal = this.User as ClaimsPrincipal;

var authenticated = this.User.Identity.IsAuthenticated;

var claims = this.User.Identities.FirstOrDefault().Claims;

var id = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
Run Code Online (Sandbox Code Playgroud)

我能够检查用户是否是authenticated并获取claims包括id.

我怎么能Controller在我没有的地方之外做同样的事情this.User

c# asp.net-core asp.net-core-2.2

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

了解 Identityserver4 与身份(cookies/令牌,服务器架构)

我在理解大局如何整合时遇到了一些问题。对于初学者来说,这是一个很大的话题。

当与 IdentityServer 结合时,Identity 的 cookie 及其所有默认模板/页面在发布令牌时有何意义?

为什么即使在使用 SPA 客户端时,这些页面也包含在所有示例中?能够通过 API 授权/认证/注册不是重点吗?

我看到很多人建议在它自己的项目中应该有IdentityServer,然后在本地网络上单独的用户数据库,这怎么可能?我还没有找到这样的样本。

为什么需要将用户数据库与资源 API 分开,并且 IdentityServer4 和 Identity 的组合不是它自己的 API?

我感谢您的时间和帮助。

c# asp.net-identity asp.net-core identityserver4

4
推荐指数
2
解决办法
800
查看次数

发生未处理的异常:找不到模块“@angular-devkit/build-angular”

在运行终端命令 ng server 或 ng serve 时,我遇到了这个问题:

发生未处理的异常:找不到模块“@angular-devkit/build-angular”

javascript node.js single-page-application typescript angular

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

SyntaxError:在 Angular8 中 JSON.parse 的位置 0 处出现意外的 JSON 标记 U

我只是避免了这个问题,直到最近当我更改以前代码的一些位和部分时,才注意到这个特定的错误。

\n\n

我使用 Angular 8、NodeJS、express 和 mongoose

\n\n

每当我尝试传递特定对象或该对象的一部分时,我都会收到此错误(来自 chrome 控制台):

\n\n
HttpErrorResponse {headers: HttpHeaders, status: 201, statusText: "Created", url: "http://localhost:3200/users/", ok: false, \xe2\x80\xa6}\nerror: {error: SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt\xe2\x80\xa6, text: "Updated successfully!"}\nheaders: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: \xc6\x92}\nmessage: "Http failure during parsing for http://localhost:3200/users/"\nname: "HttpErrorResponse"\nok: false\nstatus: 201\nstatusText: "Created"\nurl: "http://localhost:3200/users/"\n__proto__: HttpResponseBase\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是错误对象内部:

\n\n
message: "Unexpected token U in JSON at position 0"\nstack: "SyntaxError: Unexpected token U in JSON at …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js typescript angular angular8

3
推荐指数
1
解决办法
5965
查看次数