在角度6中找不到服务名称

时间:2018-11-22 08:28:44

标签: angular service

我是angular 6的新手,我编写了2个自定义服务,一个用于身份验证,一个用于从Node.js获取帖子。 服务对我来说很好用,但是在 angular-cli 中,它给了我以下错误

src/app/user/posts/posts.component.ts(27,3): error TS2304: Cannot find name 'service'.

这是我的工作资料库的结构 Structure of working repository 这是我的组件文件的代码

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.log("this")
    this.loggedin=localStorage.getItem('id');
    this.getPosts();
}

getPosts=function(){
    service.getPosts().subscribe(data=>{
        this.allpost=data;
    })
}
upload=function(d){
    if(d){
        console.log(d)
        service.createPost(d).subscribe(data=>{
          this.allpost.push(data);
          // console.log(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.log(data);
    });
    console.log(post)
}
setComments=function(id,){
    this.coment=true;
    this.setcoment=false;
    service.setComments(id,function(data){
        service.pushComments(data);
    });

}
}

2 个答案:

答案 0 :(得分:0)

就像@ mr.void已评论一样,呼叫需要带有this

将所有呼叫从service.{anything}更改为this.service.{anything}

答案 1 :(得分:0)

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.log(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.log(data);
});
console.log(post)
}
setComments=function(id,){
this.coment=true;
this.setcoment=false;
this.service.setComments(id,function(data){
    this.service.pushComments(data);
});

 }
}
  • 使用“ this”关键字来引用它所属的对象。

  • 在方法中,“ this”是指所有者对象。

相关问题