Angular 4:如何操纵"订户"元件

时间:2017-08-18 18:20:29

标签: angular

我正在重构我的代码,并且我正在使用下一个代码:

perfiles.component.ts

this._ps.getPerfiles();

perfiles.services.ts

getPerfiles(){
    let path = "perfil"
    let dat = this._request.get(path);
    console.log(dat); // <-- Here appears the bottom message
    return dat;
  }

request.service.ts

get( path:string ){
    let token = this._ls.getToken();
    return this.http.get(`${this.sett.url}/${path}?token=${token}`)
                    .map(res => {
                      return res.json();
                    })
                    .subscribe(
                      data =>{
                        return data.data;
                      },
                      error =>{
                        this.handleError(error); // this function doesn't have any problem
                      }
                    );
  }

我的问题是下一个:我收到了一个&#34;订阅者&#34;元素,但我不知道如何使用它。我如何获取数据?

请点击下一张图片以查看问题。

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须使用组件中的subscribe:

<强> perfiles.component.ts

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MyAssembly")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MyAssembly")]
[assembly: AssemblyCopyright("Copyright ©  2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2fda4fb1-6855-4af5-a0a3-fbe861dcc734")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Allow moq to access internal constructors of entities
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

<强> perfiles.services.ts

this._ps.getPerfiles().subscribe(anything => { 
  console.log(anything);
});

<强> request.service.ts

getPerfiles(){
    let path = "perfil"
    return this._request.get(path);
}

这样你就会尊重Angular的层次。