1

Why do i get cannot find object?

I get the following error :

core.service.ts (19,23): Cannot find name 'object'.

in line:

 userChange: Subject<object> = new Subject<object>();

i do have these imports:

import { Injectable } from '@angular/core';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
import {Observable, } from 'rxjs/Observable';
import { Comment } from './models/comment'
import 'rxjs/add/operator/map';
import 'rxjs/Rx';

import {Subject} from 'rxjs/Subject';

import { Router, NavigationEnd, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

@Injectable()
export class CoreService {

  constructor(private http: Http, private router: Router) { }

   userChange: Subject<object> = new Subject<object>();


further...
martin
  • 93,354
  • 25
  • 191
  • 226
maria
  • 207
  • 5
  • 22
  • 56

3 Answers3

2

You probably meant Object with a capital O. JavaScript / Typescript is case sensitive.

userChange: Subject<Object> = new Subject<Object>();
Igor
  • 60,821
  • 10
  • 100
  • 175
1

You have to use <any>.

userChange: Subject<any> = new Subject<any>();

Here you find the any documentation, in the example code you will see the difference between using Object and any.

I would approach your code this way (personal preference):

userChange$: Observable<any>;  // We declare the Observable

private userChangeSubject = new Subject<any>();  // We declare the Subject

constructor(private http: Http, private router: Router) {
     this.userChange$ = this.userChangeSubject.asObservable();  // We 'associate' the Subject with the Observable
}

updateUser(someUserParams) {
        this.userChangeSubject.next(someUserParams); // We then proceed to apply our logic and anyone subscribe to 'userChange$' will receive these someUserParams when this method is triggered
}
SrAxi
  • 19,787
  • 11
  • 46
  • 65
  • thanks!. Im new to angular 2, but have done some projects before on angular1. Could we have a talk? – maria May 09 '17 at 11:17
  • Do you have a channel for more discussion? – maria May 09 '17 at 13:05
  • because of my 150 rep lost to another topic bounty i get the following: You need 20 reputation to post messages – maria May 09 '17 at 13:09
  • @maria means that you need more reputation to chat. Anyway, if you have more trouble with your developing keep creating new questions in StackOverflow and we will help you. If you want, you can tag me so I can see the question and try to give you a hand. But Really don't hesitate on using SO's platform, is one of the quickest ways to learn your path through a new language/framework. ;) – SrAxi May 09 '17 at 13:12
  • I believe i tagged you know . tagged with [tag:SrAxi] – maria May 09 '17 at 13:14
  • anyway, link: http://stackoverflow.com/questions/43762374/session-passport-wont-save – maria May 09 '17 at 13:16
  • With the new approatch here, how would i call it in the service? this.CoreService.userchange$.subscribe((value) => { }); wont work. – maria May 09 '17 at 13:26
  • Check this example about astronauts in Angular's documentation: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service . Basically, you have **component1** calling the method in the shared service, in our case: `updateUser()`. **Component2** is listening with the above code you just commented, and receives the parameters or whatever `updateUser()` is returning. – SrAxi May 09 '17 at 13:32
0

The object type was added in TypeScript 2.2, are you sure you're using the right TypeScript version?

For more info see: What's-new-in-TypeScript#object-type

martin
  • 93,354
  • 25
  • 191
  • 226