我需要将tabs.ts中的值传递给每个标签页。所以我有这样的事情:
constructor(public navParams: NavParams) {
...// config
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// If there's a user take him to the home page.
this.user = [];
this.user.push(user);
this.rootPage = HomePage;
} else {
// If there's no user logged in send him to the LoginPage
this.rootPage = LoginPage;
}
});
}
this.tab1Root = HomePage;
this.tab4Root = ProfilePage;
如何将值(用户)传递给每个标签页?我尝试了几个这个代码的组合,但没有工作(得到一些错误 - 例如,如果我把this.tab1Root ...添加到onAuthStateChanged方法,然后它给了我:“超出最大调用堆栈大小”)。以下是文档:http://ionicframework.com/docs/v2/api/components/tabs/Tab/ - 我理解其中的90%,但仍然不知道我应该如何传递这个值......
我的第二个问题 - 是否有更好的方法来吸引当前用户并将其作为价值传递到每个页面?如果我使用提供商或其他什么会更好?
第三个问题:在tabs.ts中使用此代码比使用app.ts更好吗?
谢谢!
答案 0 :(得分:1)
您可以在ion-tab
中使用[rootParams]属性<ion-tab ... [rootParams]="user"></ion-tab>
在标签文件中:
constructor(navParams: NavParams) {
console.log("Passed params", navParams.data.user);
}
第二种方式是使用事件:http://ionicframework.com/docs/v2/api/util/Events/ 它允许您在任何页面之间共享数据。
提供商是一个不错的选择。