【问题标题】:pass data to sidemenu Ionic将数据传递给sidemenu Ionic
【发布时间】:2019-04-13 20:37:01
【问题描述】:

我使用 fb 登录,并在确认后重定向到主页,但是我想将数据传递给 App.html,因为我的 sideMenu 在其中。但我找不到将数据传递给 app.component.ts 的方法 谁能告诉我如何获取侧面菜单的数据?

【问题讨论】:

  • 您需要有一个共享的提供者(服务),应用程序、任何页面或组件都可以导入/注入和访问 n xchange 数据。有关使用共享服务的组件交互,请参阅 angular.io 文档

标签: angular cordova ionic-framework


【解决方案1】:

使用 ionic 的 events 插件

在您的 app.component.ts 文件中创建一个函数或将下面的代码放入平台中,以便监听任何已发布的事件

  events.subscribe('user:created', (user, time) => {
    // user and time are the same arguments passed in `events.publish(user, time)`
    console.log('Welcome', user, 'at', time);
  });

在您的login.ts 文件中成功登录后使用下面的代码发布数据

this.events.publish('user:created', user, Date.now());

更多信息请阅读events插件

【讨论】:

  • 感谢回复,我相信我已经成功了,但是我不知道如何接收html中的用户名。但在 console.log 中我能够看到正在传递的数据。
  • app.component.ts: this.createUser(events); createUser(events) { events.subscribe('user:created', (user, time) => { // user 和 time 是在 events.publish(user, time)console.log('Welcome', user, 'at' 中传递的相同参数, 时间); console.log(user); }); } 在我的控制台中,我看到我收到了 User 的值
  • 所以你在app.component.ts variable:any; 中声明全局变量events.subscribe('user:created',(user,time)=>{ this.variable = user; });然后你可以通过使用 {{ variable }} 在 html 中获取用户
  • 谢谢,我得到了几乎所有东西,但是在 html 中,当我插入 {{user}} 时,它会返回 html [Object, Object]
  • 用户包含什么值? user 是一个对象,它打印[Object, Object]
猜你喜欢
  • 2017-07-30
  • 2021-06-17
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 2017-06-16
  • 2021-09-20
相关资源
最近更新 更多