我有Todo组件类。执行ng服务时我可以运行项目,但在命令提示符下看到错误:
import { Component, OnInit } from '@angular/core';
import { TodoDataService } from '../service/data/todo-data.service';
import { Todo } from '../list-todos/list-todos.component';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-todo',
templateUrl: './todo.component.html',
styleUrls: ['./todo.component.css']
})
export class TodoComponent implements OnInit {
id:number
todo:Todo
constructor(
private todoService: TodoDataService,
private route:ActivatedRoute,
private router :Router
) { }
ngOnInit() {
this.id=this.route.snapshot.params['id'];
this.todo=new Todo(this.id,'',false,new Date());
if(this.id != -1){
this.todoService.retrieveTodo('in28minutes',this.id)
.subscribe(
data => this.todo=data
)
}
}
saveToDo(){
console.log("before entering"+this.id);
if(this.id == -1){
//create todo
console.log("here at create");
this.todoService.createTodo('in28minutes',this.todo).subscribe(
data => console.log(data)
this.router.navigate(['todos'])
)
}
else{
//update todo
console.log(this.id);
console.log("here at update");
this.todoService.updateTodo('in28minutes',this.id,this.todo).subscribe(
data => console.log(data)
this.router.navigate(['todos'])
)
}
}
}
在编译和运行时出现错误,我复习了此类,但在那里没有看到任何逗号。
答案 0 :(得分:2)
我认为这是因为您使用订阅的方式,它必须是一个函数,但是由于您没有将代码包装到{}中,因此它被视为两个参数(响应和错误)。您应该看看documentation。
该编译告诉您有两个错误,并且看起来这两个错误的代码完全相同。
替换为您的订阅:
this.todoService.createTodo('in28minutes',this.todo).subscribe(data => {
console.log(data)
this.router.navigate(['todos'])
}
);