标签: angular2-services

主题在构造函数中返回undefined

如果用户是否经过身份验证,我正在使用Subject来进行通信.问题是,当我在重新加载时订阅组件中的主题时,主题返回undefined.

这是我的实现,此方法检查用户数据是否存储在本地存储中.如果它们存储在本地存储中,则Subject应设置为true.

user.service.ts

public auth_status:Subject<boolean> = new Subject<boolean>();

getCurrentUser(): User {
    let storage =  localStorage.getItem('current_user');

    if (storage) {
        return Json.parse(storage);
    } else {
        return null
    }
};

getUserAuth() {
    if (this.getCurrentUser() != null) {
        this.auth_status.next(true);
        console.log(this.auth_status);
    } else {
        this.auth_status.next(false);
        console.log(this.auth_status);
    }
    return this.auth_status;
}
Run Code Online (Sandbox Code Playgroud)

navbar.component.ts

  constructor(private _userService: UserService) {
    this._userService.getUserAuth().subscribe(data => this.auth_status = data);
  }
Run Code Online (Sandbox Code Playgroud)

this.auth_status在我的导航栏中切换*ngIf,显示登录或退出按钮

我已经尝试在订阅它之前调用该方法,但这似乎不起作用.

谢谢!

observable typescript angular2-services angular

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

如何在内存Web API中使用两个不同的JSON

我正在为两个json服务(DB)使用In memory Web API

1)

import {InMemoryDbService} from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let heroes = [
            {id: 11, name: 'Mr. Nice'},
            {id: 12, name: 'Narco'},
            {id: 13, name: 'Bombasto'},
            {id: 14, name: 'Celeritas'},
            {id: 15, name: 'Magneta'},
            {id: 16, name: 'RubberMan'},
            {id: 17, name: 'Dynama'},
            {id: 18, name: 'Dr IQ'},
            {id: 19, name: 'Magma'},
            {id: 20, name: 'Tornado'}
        ];
        return {heroes};
    }
}      
Run Code Online (Sandbox Code Playgroud)

2)

import {InMemoryDbService} from 'angular-in-memory-web-api';

import {Address} from "cluster";

export class StudentData …
Run Code Online (Sandbox Code Playgroud)

angular2-template angular2-routing angular2-services angular

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

无法访问组件中的服务变量 - Angular2

我正在服务中进行 HTTP 调用并将返回数据分配给服务变量。现在,当我尝试访问组件中的服务变量时,它在控制台中记录为未定义。但是,当我将日志代码放入服务本身时它会被记录,但在组件中它不起作用。

下面是我的代码供参考:

英雄服务

import { Injectable }              from '@angular/core';
import { Http, Response }          from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { Hero } from './hero';

@Injectable()
export class HeroService {
heroes: Hero[];
hero: Hero;
gbl: Hero[];

  private heroesUrl = 'SERVICE URL';
  constructor (private http: Http) {}

  getHeroes(): Observable<Hero[]> {
    return this.http.get(this.heroesUrl)
                    .map(this.extractData)
                    .catch(this.handleError);
  }
  private extractData(res: Response) {
    let body = res.json()['data'];
    this.gbl = body;
    return body || { };

  } …
Run Code Online (Sandbox Code Playgroud)

typescript angular2-routing angular2-services angular2-observables angular

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

我如何使用 ngRedux 测试服务

我如何模拟 ngRedux 的 getState:使用 karma jasmine

例如,我的服务通过 ngRedux 从商店获取价值。

this.ngRedux.getState()
Run Code Online (Sandbox Code Playgroud)

jasmine typescript karma-jasmine angular2-services angular

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

无法读取未定义的属性“setRowData”

我正在尝试在我的 ag 网格上获取新数据。我尝试使用

this.gridOptions.api.setRowData(rowdata);
Run Code Online (Sandbox Code Playgroud)

但它抛出这个错误

无法读取未定义的属性“setRowData”

我把这个方法保存在onGridReady方法处onInit。不确定这是否有帮助,但我cellRenderer也在使用。

这种方法对其他人有用吗?

有什么特别的东西可以调用这个方法吗?

ag-grid angular2-services

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

如何将接口作为参数传递并在 TypeScript 中获得匿名回调?

这是代码,我试图实现:

this._customHttp.httpPostService('app/fetchGridDetails',this.req,new  
  InterfaceName() {
         onEvent(str :string) {
            console.log(str);
         }
    });
Run Code Online (Sandbox Code Playgroud)

typescript angular2-services angular

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

无法读取未定义的角度 2 的属性“用户名”

我尝试了很多解决方案,但没有一个能帮助我解决这个问题......无法弄清楚我的代码中有什么问题......

HTML 代码 ::

<input type="text" class="form-control"  placeholder="Please Enter Your Username" [(ngModel)]="currentUser.UserName" minlength="3" required id="userNameBox" name="UserName" #uName="ngModel">
Run Code Online (Sandbox Code Playgroud)

组件代码 ::

    profile() {
        let uid = Number(sessionStorage.getItem('id'));
        this._homeService.profile(uid).subscribe(
            res => {
            this.currentUser = res

                console.log(this.currentUser);

});
Run Code Online (Sandbox Code Playgroud)

服务 ::

profile(id: number) {

        return this._http.get(url, id).map(
            (res: Response) => {
                console.log(res.json())
                return new User(res.json()

                ) }
        ).catch(this.handleError);
    }
Run Code Online (Sandbox Code Playgroud)

模型 ::

export class User {

constructor(json: any) {
    if (json != null) {
        this.Id = json.Id;
        this.UserName = json.UserName;
        this.FirstName = json.FirstName;
        this.LastName = json.LastName; …
Run Code Online (Sandbox Code Playgroud)

angular2-forms angular2-template angular2-services angular

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

订阅 Observable 与订阅 Subject

在 Angular 应用程序中,有多种方法可以从服务器获取数据:

  1. 从服务中获取 Observable 并在组件处订阅它
  2. 在服务上创建主题并在组件上订阅主题

这两种方法都对我有用,但我不明白我应该使用哪种方法。

第一种方法从服务中获取 Observable 并在组件处订阅它

article.service.ts

import { Injectable } from '@angular/core';
import { Article } from '../models/article';
import { AngularFirestore } from '@angular/fire/firestore';
import { map, take } from 'rxjs/operators';
import { Observable, Subject } from 'rxjs';

@Injectable({
  providedIn: "root"
})
export class ArticleService {
  public articlesChanged: Subject<Article[]> = new Subject<Article[]>();
  articles: Article[];

  constructor(private db: AngularFirestore) {}

  get() {
    return this.db.collection('articles').valueChanges({ idField: 'id' });
  }
}
Run Code Online (Sandbox Code Playgroud)

home.component.ts

import { Component, …
Run Code Online (Sandbox Code Playgroud)

firebase angular2-services angular

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

解决承诺angular2:类型'[]'上不存在属性'包含'

我收到了错误

app/hero-detail.component.ts(44,29): error TS2339: Property 'includes' does not exist on type '[]'.
app/hero.service.ts(25,25): error TS1122: A tuple type element list cannot be empty.
Run Code Online (Sandbox Code Playgroud)

代码将无法编译和运行,此错误已启用npm start.

hero.service.ts:

import { Injectable } from '@angular/core';

import { Hero, HeroIds } from './hero';
import { HEROES } from './mock-heroes';

@Injectable()
export class HeroService {

  getHeroes(): Promise<Hero[]> {
    return Promise.resolve(HEROES);
  }

  getHero(id: number): Promise<Hero> {
    return this.getHeroes()
        .then(heroes => heroes.find(hero => hero.id === id));
  }

  getHeroesSlowly(): Promise<Hero[]> {
    return new Promise(resolve => { …
Run Code Online (Sandbox Code Playgroud)

javascript angular2-services angular

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

ng build命令无法通过jenkins build execute shell运行

在jenkins构建执行区域中,我输入了以下命令:

cd /var/lib/jenkins/workspace/test/
ng serve
Run Code Online (Sandbox Code Playgroud)

这是屏幕截图:

屏幕截图

我收到这样的错误:

  • cd / var / lib / jenkins / workspace / test /
  • ng服务

未定义环境变量TERM!

构建步骤“ Execute shell”将构建标记为失败完成:FAILURE

节点v6.10.0

@角度/ cli:1.0.6

请帮助我解决此问题。

shell sh jenkins angular2-services

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