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

问题描述 投票:-1回答:1

我有一个问题,我想将对象列表从服务传递到组件。响应是不确定的。

这是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;
  }
}

这是组件view-item.component.ts]的ts

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);
  }
}

现在我只需要对象的console.log。

谢谢!

我有一个问题,我想将对象列表从服务传递到组件。响应是不确定的。这是data-service.service:从'@ angular / core'导入{Injectable}; @Injectable({...

typescript object service angular8
1个回答
0
投票

您的代码中有很多语法错误。

© www.soinside.com 2019 - 2024. All rights reserved.