Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON对象传递到Angular 2组件_Json_Angular - Fatal编程技术网

将JSON对象传递到Angular 2组件

将JSON对象传递到Angular 2组件,json,angular,Json,Angular,我需要使用RxJS Subject和observates将JSON对象从一个组件传递到另一个组件。这是我的密码: meta-form.component.ts import { SharedService } from "../../../shared/shared.service"; @Component({ selector: 'meta-form', template: require("./meta-form.component.html"), styles:

我需要使用RxJS Subject和observates将JSON对象从一个组件传递到另一个组件。这是我的密码:

meta-form.component.ts

import { SharedService } from "../../../shared/shared.service";

@Component({
    selector: 'meta-form',
    template: require("./meta-form.component.html"),
    styles: [require("./meta-form.component.css")],
    providers: [SharedService]
})
export class MetaFormComponent implements OnInit {
    public metaForm: FormGroup;

    constructor(private _fb: FormBuilder, private sharedService: SharedService) { }

    ngOnInit() {
        this.metaForm = this._fb.group({
            params: this._fb.array([
                this.initParam(),
            ])
        })
    }

    initParam(): any {
        return this._fb.group({
            description: ['', Validators.required],
            type: ['', Validators.required]
        })
    }

    sendJSON() {
        let json = JSON.stringify(this.metaForm.value);
        this.sharedService.MetaData(json);
    }
}
保存格式组件

import { SharedService } from "../../../shared/shared.service";

import 'rxjs/Rx';

@Component({
    selector: 'save-form',
    template: require("./save-form.component.html"),
    styles: [require("./save-form.component.css")],
    providers: [SharedService]
})
export class SaveFormComponent implements OnInit {
    schema: string[];

    constructor(private sharedService: SharedService) {
        sharedService.metaData$.subscribe((res) => this.schema = res);
    }

    ngOnInit() {
    }
}
和shared.service.ts

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

@Injectable()
export class SharedService {

    private metaData = new Subject<string[]>();
    metaData$ = this.metaData.asObservable();

    MetaData(jsonData) {
        this.metaData.next(jsonData);
    }
}
我做错了什么

您正在
@Component.providers

@Component({
    providers: [SharedService]
})
这意味着您希望该组件获得自己的服务实例。因此,当创建组件时,将创建一个新服务

如果希望它们共享相同的服务,则在模块级别声明该服务


日志中有错误吗?另外,您可以介绍您如何打印JSON以及JSON实际形状的示例吗?@silentsod updatedIn
SaveComponent
schema
真的是一个字符串数组,还是应该只是一个字符串?看起来,可观测对象只是在推出一个字符串。
{
  "params": [
    {
      "description": "desc1",
      "type": "string"
    }
  ]
}
@Component({
    providers: [SharedService]
})
@NgModule({
    providers: [ SharedService ]
})
class AppModule {}