首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试用角/本地文本路由传递JSON数据

尝试用角/本地文本路由传递JSON数据
EN

Stack Overflow用户
提问于 2018-01-19 22:38:00
回答 1查看 511关注 0票数 1

我目前正在进行一个本地转录/角形/类型转换项目,我基本上是试图将JSON数据从一个视图(属性)传递到另一个视图(propertyinfo)。

首先,我在property.service.ts中加载了一个JSON文件

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';

@Injectable()
export class PropertyService {
    public propertyData: any;
    public selectedProperty: any;

    constructor(private http: HttpClient) {
        this.loadProperties();
    }

    loadProperties() {
        this.http.get('/pages/property/property.json').subscribe(
            (data) => {
                this.propertyData = data;
            }
        )
    }
}

此JSON数据显示在视图property.component.html

代码语言:javascript
复制
<StackLayout *ngFor="let item of propertyData" class="list-group" xstyle="height: 300">
<GridLayout rows="110" columns="*, 40" (tap)="details(item)">
    <StackLayout row="0" col="0">
        <Label text="{{item.streetName}} {{item.houseNumber}}" class="text-primary p-l-30 p-t-5"></Label>
        <Label text="{{item.etc}} {{item.etc}}" class="text-primary p-l-30 p-t-5"></Label>
    </StackLayout>
    <Label row="0" col="1" text="&#xf054;" class="fa arrow" verticalAlignment="middle"></Label>
</GridLayout>
<StackLayout class="hr-light"></StackLayout>

在这里,property.component.ts中的(tap)="details(item)"将调用一个函数

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import { PropertyService } from './property.service';

@Component({
    selector: 'app-property',
    templateUrl: 'pages/property/property.component.html',
    providers: [PropertyService]
})

export class PropertyComponent implements OnInit {
    public propertyData: any;

    constructor(private router: Router, private propertyService: PropertyService) {}

    ngOnInit() {
        this.propertyData = this.propertyService.propertyData;
    }

    details(item: any) {
        this.propertyService.selectedProperty = item;
        this.router.navigate(["/propertyinfo"]);
    }
}

现在,当我在details函数中执行console.log(JSON.stringify(this.propertyService.selectedProperty));时,输出如下:

代码语言:javascript
复制
JS: {"ID":4,"description":"Lorem ipsum dolor sit amet...", "streetName":"Somestreet","houseNumber":459,"etc":"etc"}

这是我的propertyinfo.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { PropertyService } from '../property/property.service';
import { Router } from '@angular/router';    

@Component({
  selector: 'app-propertyinfo',
  templateUrl: 'pages/propertyinfo/propertyinfo.component.html'
})

export class PropertyinfoComponent implements OnInit {
  public selectedProperty: any;

  constructor(private propertyService: PropertyService, private router: Router) { 
    this.selectedProperty = this.propertyService.selectedProperty;
  }

  ngOnInit() { }    
}

但是,当我在构造函数中执行console.log(JSON.stringify(this.selectedProperty));时,输出是JS: undefined

在这篇文章的末尾,我将添加app.routing.ts和app.module.ts文件,这样您就可以看到我的所有导入/指令等等。我希望你能帮助我。

app.routing.ts

代码语言:javascript
复制
import { NgModule } from "@angular/core";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { Routes } from "@angular/router";

import { PropertyComponent } from "./pages/property/property.component";
import { PropertyinfoComponent } from ".//pages/propertyinfo/propertyinfo.component";

const routes: Routes = [
    { path: "", component: PropertyComponent },
    { path: "propertyinfo", component: PropertyinfoComponent },
];

@NgModule({
    imports: [NativeScriptRouterModule.forRoot(routes)],
    exports: [NativeScriptRouterModule]
})
export class AppRoutingModule { }

app.module.ts

代码语言:javascript
复制
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { AppRoutingModule } from "./app.routing";
import { AppComponent } from "./app.component";

import { PropertyService } from "./pages/property/property.service";
import { PropertyComponent } from "./pages/property/property.component";

import { PropertyinfoComponent } from "./pages/propertyinfo/propertyinfo.component";

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        NativeScriptRouterModule,
        HttpClientModule,
        NativeScriptHttpClientModule,
    ],
    declarations: [
        AppComponent,
        PropertyComponent,
        PropertyinfoComponent
    ],
    providers: [
        PropertyService
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})

export class AppModule { }

谢谢你提前提供帮助。如果我需要澄清/提供更多的信息,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-20 03:36:43

console.log(JSON.stringify(this.selectedProperty))在PropertyinfoComponent中返回未定义的内容,因为注入的PropertyService服务在PropertyinfoComponent中与在PropertyComponent中的实例不同。

您没有为这两个组件发布完整的.html,所以我只能假设您有一个类似于PropertyComponent是父组件的东西,并且在html中有一个引用/包含一个PropertyinfoComponent,并且由于它的角度工作方式,它注入了一个新的服务实例,而不是使用来自父组件的一个实例。

有关这类问题的角度服务注入的更多信息,请查看Templier的答案:How do I create a singleton service in Angular 2?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48350567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档