에 서비스를 통해 데이터를 가져 오는 경우, 내가 해결할 수없는 것 오류.형식 오류 정의되지 않은 변수 I는 각도 5</p> <p>내가 시간 만 아무 소용 위해 노력 해왔다에서 학습 CRUD 작업의 단계에서 막혔어요 요소
서비스에서 가져온 데이터를 구독하고 푸시를 사용하여 데이터를 삽입하려고 할 때. 그것은 정의되지 않는다.
addtasks.component.ts
import { Component } from '@angular/core';
import { AddTaskService } from '../../services/add-task.service';
import { Tasks } from '../../../Tasks/Tasks';
@Component({
selector: 'app-addtasks',
templateUrl: './addtasks.component.html',
styleUrls: ['./addtasks.component.css']
})
export class AddtasksComponent {
tasks: Tasks[];
title: string;
constructor(private addTaskService: AddTaskService) {
console.log("Add tasks page has been accessed...");
}
addTasks(event){
event.preventDefault();
var newTask = {
title: this.title,
isDone: false
};
this.addTaskService.addTask(newTask).subscribe(task => {
this.tasks.push(task);
this.title = '';
});
}
}
추가 task.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class AddTaskService {
constructor(private http: Http) { console.log("Add Task service initialized..."); }
addTask(newTask){
var headers = new Headers();
headers.append('Content-Type', 'application/json');
console.log(newTask);
return this.http.post('http://localhost:2200/api/tasks', JSON.stringify(newTask), {headers: headers})
.map(res => res.json());
}
}
모델
Tasks.ts
export class Tasks {
title: string;
isDone: boolean;
}
내 콘솔
형식 오류 columnNumber에 나타내는 에러 13 파일명 : "http://localhost:2200/main.bundle.js"LINENUMBER 193 메시지 : 스택 "_this.tasks 정의되지 않은" 를 "../. ./../../../src/app/components/addtasks/addtasks.component.ts/AddtasksComponent.prototype.addTasks/< @http://localhost:2200/main.bundle.js:193:13 \ n ../../../../ rxjs/_esm5/Subscriber.js/SafeSubscriber.prototype. tryOrUnsub @http://localhost:2200/vendor.bundle.js:1697:13 \ n ../../../../ rxjs/_esm5/Subscriber.js/SafeSubscriber.prototype.next @http://localhost:2200/vendor.bundle.js:1644:17 \ n ../../../../ rxjs/_esm5 /Subscriber.js/[email protected]http://localhost:2200/vendor.bundle.js:1585:9 \ n ../../../../ rxjs/_esm5/Subscriber.js/Subscriber.prototype.next @http://localhost:2200/vendor.bundle.js:1549:13 \ n ../../. ./../rxjs/_esm5/operators/map.js/[email protected]http://localhost:2200/vendor.bundle.js:4978:9 \ n ../../../../ rxjs/_esm5/Subscriber.js/Subscriber.prototype.next @http://localhost:2200/vendor.bundle.js:1549:13 \ nonLoad @http://localhost:2200/vendor.bundle.js:75626:21 \ n ../../../../ zone.js/dist/zone.js/http : // localhost : 2200/polyfills.bundle.js : 2504 : 17 \ nonInvokeTask @http://localhost:2200/vendor.bundle.js:53623:24 \ n ../../../../ zone.js/dist/zone.js/http : // localhost : 2200/polyfills.bundle.js : 2503 : 17 \ n ../../. ./../zone.js/dist/zone.js/http://localhost:2200/polyfills.bundle.js:2271:28\n../../../../zone.js/ dist/zone.js/http : // localhost : 2200/polyfills.bundle.js : 2578 : 24 \ ninvokeTask @http://localhost:2200/polyfills.bundle.js:3619:9 \ nglobalZoneAwareCallback @http://localhost:2200/polyfills.bundle.js:3645:17 \ n " __proto : 개체 {스택 :" ", ...}
네,이 말이 맞습니다. 생성자에서 할 필요는 없지만 속성을 정의 할 수 있습니다. 'tasks : 작업 [] = [];' –