简体   繁体   English

如何将 object 从服务传递到 Angular8 中的组件?

[英]How to pass an object from a service to a component in Angular8?

I have a problem i want to pass an object list from a service to a component.我有一个问题,我想将 object 列表从服务传递给组件。 the response is undefined.响应未定义。

This is the data-service.service :这是data-service.service

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class DataService {
  public data: {
    "first": [
      {
        list: 'first';
        id: 1;
        title: 'First list title item';
        description?: 'Example description for first list title item';
        completed: false;
      }
    ],
    "second": [
      {
        list: 'second'
        id: 2;
        title: 'Second list title item';
        description?: 'Example description for second list title item';
        completed: false;
      }
    ],
    "third": [
      {
        list: 'third'
        id: 2;
        title: 'Third list title item';
        description?: 'Example description for third list title item';
        completed: false;
      }
    ],
  };
  loadAll(){
    return this.data;
  }
}

This is the ts of component view-item.component.ts这是组件view-item.component.tsts

import { Component, OnInit } from '@angular/core';
import { DataService } from '../service/data-service.service';

@Component({
  selector: 'app-todo-item',
  templateUrl: './view-item.component.html',
  styleUrls: ['./view-item.component.scss'],
})

export class ViewItemComponent implements OnInit {
  todos: {};
  constructor(private dataService: DataService) {}

  ngOnInit() {
    this.todos = this.dataService;
    this.dataService.loadAll();
    console.log(this.dataService.data);
  }
}

for now I need only the console.log of the object.现在我只需要 object 的 console.log。

Thanks!谢谢!

There are a bunch of syntax errors in your code.您的代码中有一堆语法错误。

1) You aren't assigning anything to data in your service. 1)您没有为服务中的data分配任何内容。

2) The keys/properties should be separated by comma, not semicolon. 2) 键/属性应该用逗号分隔,而不是分号。

This should fix it.这应该解决它。

On your data-service.service.ts,在您的 data-service.service.ts 上,

@Injectable()
export class DataService {
  public data = {
    "first": [
      {
        list: 'first',
        id: 1,
        title: 'First list title item',
        description: 'Example description for first list title item',
        completed: false,
      }
    ],
    "second": [
      {
        list: 'second',
        id: 2,
        title: 'Second list title item',
        description: 'Example description for second list title item',
        completed: false,
      }
    ],
    "third": [
      {
        list: 'third',
        id: 2,
        title: 'Third list title item',
        description: 'Example description for third list title item',
        completed: false,
      }
    ],
  };
  loadAll(){
    return this.data;
  }
}

And on your component.ts, you should assign the values returned by the loadAll() method from your service into a variable or property in your component.在您的 component.ts 上,您应该将服务中的loadAll()方法返回的值分配给组件中的变量或属性。

import { Component, OnInit } from '@angular/core';
import { DataService } from '../service/data-service.service';

@Component({
  selector: 'app-todo-item',
  templateUrl: './view-item.component.html',
  styleUrls: ['./view-item.component.scss'],
})

export class ViewItemComponent implements OnInit {
  todos;
  constructor(private dataService: DataService) {}

  ngOnInit() {
    this.todos = this.dataService.loadAll();
    console.log(this.todos);
  }
}

I have reproduced a demo over here .我在这里复制了一个演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Angular8 中的另一个组件传递一个组件的值(独立组件) - How can I pass value from one component from another component in Angular8 (Independent components) Angular8 - 从服务,排序和显示返回的对象 - Angular8 - object returned from service, sort and display Angular 6如何从BehaviourSubject传递选定的对象 <Object[]> 组件使用服务? - Angular 6 how to pass selected Object from BehaviourSubject<Object[]> to component using service? 如何以角度将对象从服务返回到组件 - How to return object from service to component in angular 如何从Angular8中的object数组中删除选定的行 - How to delete selected rows from array of object in Angular8 如何在angular8中添加对扩展服务的依赖 - How to add dependency to extended service in angular8 如何在angular8中为IformFile传递上传数据 - How to pass upload data for IformFile in angular8 如何在 Angular8 中创建通用请求服务 - How to make a generic request service in Angular8 Angular8 - 可观察的内部服务,如何根据逻辑代码条件通知订阅的组件? - Angular8 - observable inside service, how to notify the subscribed component based on logical code condition? 如何使用 angular8 将内嵌可编辑的 mat-table 数据传递到服务中? - How to pass the in-line editable mat-table data into the service using angular8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM