当前位置: 首页 > 知识库问答 >
问题:

共享服务无法将数据传递到下一个组件

毛镜
2023-03-14
import { Component, OnInit } from '@angular/core';
import {SharedService} from './../shared.service';
import { Router, NavigationStart } from '@angular/router';



    @Component({
      selector: 'app-cone',
      templateUrl: './cone.component.html',
      styleUrls: ['./cone.component.css'],
      providers: [SharedService]
    })
    export class ConeComponent implements OnInit {
    req = <any>{};
      constructor(public service:SharedService,private router:Router) { }
       send(){
        this.req.fname= "ketan";
        this.req.lname= "pradhan";
        this.service.saveData(this.req); 
        console.log('str');
        this.router.navigate(['/apps/ctwo']);
      }

      ngOnInit() {

      }

    }
import { Component, OnInit } from '@angular/core';
import {SharedService} from './../shared.service';
import { Router, NavigationStart } from '@angular/router';

@Component({
  selector: 'app-ctwo',
  templateUrl: './ctwo.component.html',
  styleUrls: ['./ctwo.component.css'],
  providers: [SharedService]
})
export class CtwoComponent implements OnInit {
myName= <any>{};

  constructor(public service:SharedService,private router:Router) {
    this.myName=this.service.getData();
        console.log(this.myName);
   }

  ngOnInit() {

  }

}
@Injectable()
export class SharedService {
  sharingData: myData=<any>{};
  saveData(str){
    console.log('save data function called' + str.fname);
    console.log(this.sharingData);
    this.sharingData = str; 
    // this.sharingData.lname=str.lname; 
    console.log(this.sharingData)
  }
  getData()
  {
    console.log('get data function called');
    return this.sharingData;
  }
} 

共有1个答案

冯宪
2023-03-14

当您在组件级别设置providers数组时,在本例中,这意味着您有两个独立的服务实例。

您需要在ngmoduleproviders数组中声明服务,然后这两个组件(以及该模块中的任何其他组件)将具有相同的服务实例。

因此,删除组件中的providers数组,而是将服务添加到ngmodule中的providers数组。

@Component({
  selector: 'app-ctwo',
  templateUrl: './ctwo.component.html',
  styleUrls: ['./ctwo.component.css'],
  // providers: [SharedService] // remove these!
})
@NgModule({
  imports: [ ... ],
  declarations: [ .. ],
  bootstrap: [ ... ],
  providers: [ SharedService ] // here!
})
 类似资料:
  • 我是新来的。js,并努力理解一些核心概念,以决定是否将此库用于我们的应用程序。我的主要问题实际上是在从服务器获取的模型中处理更新。 想象一下,我有一个页面应该显示五个不同的模型。我已经按照本文中描述的方式构建了它:http://facebook.github.io/react/blog/2013/11/05/thinking-in-react.html,所以我有“根”组件,所有5个模型都通过了这个

  • 我正在工作的这个应用程序运行两个不同的查询到Firebase。第一个Firebase查询显示pickerView中的数据,第二个是对“Users”集合的查询并提取自定义域。两个查询都按预期工作,pickerView显示信息,并且我能够将用户集合的结果打印到控制台。我遇到的问题是,我正在尝试将自定义域的值传递给下一个VC,但没有成功。我不确定我错过了什么。非常感谢任何帮助。下面是我的代码:

  • 我正试图将数据复制到数组中,但出现了一个错误,称无法转换类型。我该怎么解决这个问题?

  • 我从API中提取了一些产品,并将它们显示在卡中。卡片上有一个更详细的链接,如果用户单击该链接,它会将用户带到所选的产品详细信息页面。我的productDetails页面路由正常,但我很难找到将获取的数据作为道具传递到productDetails页面的方法。 这是我目前拥有的:My FeaturedProduct.js: 我的产品详细信息页面:

  • 问题内容: 我有类似的服务 我想从控制器做类似 getCurrentPosition可以正常工作,并且城市也已确定,但是我不知道如何在控制器中访问城市。 怎么了? 调用getCurrentCity时,它将调用getCurrentPosition来确定gps坐标。该坐标作为参数传递给onSuccess方法,对吗?因此,这与我要在getCurrentCity方法中执行的操作完全相同,但我不知道如何做。

  • 我在Symfony 3.3的服务中使用了一个参数,但我不断收到错误。 错误 [Symfony\Component\DependencyInjection\Exception\AutowiringFailedException]无法自动连线服务“AppBundle\service\ApiInterface”:方法“\uuu construct()”的参数“$api\u endpoint”必须具有类型