Я новичок в angular 6, я написал 2 настраиваемых сервиса, один для аутентификации и один для получения сообщений от Nodejs служба у меня работает нормально, но в angular-cli выдает следующую ошибку
src/app/user/posts/posts.component.ts(27,3): error TS2304: Cannot find name 'service'.
вот структура моего рабочего репозитория Структура рабочего репозитория и вот код моего файла компонента
import { Component,OnInit,Input } from '@angular/core';
import { PostService } from '../../post.service';
import { CommentComponent} from './comment/comment.component';
@Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.css'],
providers:[PostService]
})
export class PostsComponent implements OnInit {
loggedin='';
coment=false;
setcoment=true;
allpost=[];
update=false;
ngOnInit() {
}
constructor(private service:PostService){
// console.info("this")
this.loggedin=localStorage.getItem('id');
this.getPosts();
}
getPosts=function(){
service.getPosts().subscribe(data=>{
this.allpost=data;
})
}
upload=function(d){
if (d){
console.info(d)
service.createPost(d).subscribe(data=>{
this.allpost.push(data);
// console.info(data);
});
}
}
comment=function(post,d){
// post.push({comment:d});
// let index = this.allpost.indexOf(post._id);
post["comment"]=d;
service.createComment(post).subscribe(data=>{
})
}
deletePost=function(post){
service.deletePost(post._id).subscribe(data=>{
// for(var i=0;i<this.allpost.length;i++){
// if (this.allpost[i]._id==post._id){
// delete(this.allpost[i])
// }
// }
});
}
editPost=function(){
this.update=true;
}
updatePost=function(post){
this.update=false;
post.time=Date.now();
service.updatePost(post).subscribe(data=>{
console.info(data);
});
console.info(post)
}
setComments=function(id,){
this.coment=true;
this.setcoment=false;
service.setComments(id,function(data){
service.pushComments(data);
});
}
}
спасибо большое, это сработало для меня





Как уже прокомментировал @ mr.void, звонки должны иметь this.
Измените все вызовы с service.{anything} на this.service.{anything}.
export class PostsComponent implements OnInit {
loggedin='';
coment=false;
setcoment=true;
allpost=[];
update=false;
constructor(private service:PostService){
this.loggedin=localStorage.getItem('id');
this.getPosts();
}
getPosts=function(){
this.service.getPosts().subscribe(data=>{
this.allpost=data;
})
}
upload=function(d){
if (d){
console.info(d)
this.service.createPost(d).subscribe(data=>{
this.allpost.push(data);
});
}
}
comment=function(post,d){
post["comment"]=d;
this.service.createComment(post).subscribe(data=>{
})
}
deletePost=function(post){
this.service.deletePost(post._id).subscribe(data=>{
});
}
updatePost=function(post){
this.update=false;
post.time=Date.now();
this.service.updatePost(post).subscribe(data=>{
console.info(data);
});
console.info(post)
}
setComments=function(id,){
this.coment=true;
this.setcoment=false;
this.service.setComments(id,function(data){
this.service.pushComments(data);
});
}
}
используйте ключевое слово this, которое относится к объекту, которому оно принадлежит.
В методе «this» относится к объекту-владельцу.
this.service ....