Ionic2 tabs/app,ts传值
Ionic2 tabs/app,ts passing value
我需要将值从 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;
如何给tabs的每一页传值(user)?我尝试使用此代码的几种组合但没有用(出现一些错误 - 例如,如果我将 this.tab1Root... 放入 onAuthStateChanged 方法,那么它会给我:"Maximum call stack size exceeded")。这里是文档:http://ionicframework.com/docs/v2/api/components/tabs/Tab/ - 我理解其中的 90%,但仍然不知道我应该如何传递这个值...
我的第二个问题 - 有没有更好的方法来获取当前用户并将他作为值传递给每个页面?如果我使用 provider 之类的会更好吗?
第三个问题:这个代码在tabs.ts比在app.ts好吗?
谢谢!
您可以在 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/
它允许您在任何页面之间共享数据。
Provider 是一个不错的选择。
- 视情况而定。更好的方法是在应用程序启动时使用 app.ts 中的提供程序进行一次授权。
我需要将值从 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;
如何给tabs的每一页传值(user)?我尝试使用此代码的几种组合但没有用(出现一些错误 - 例如,如果我将 this.tab1Root... 放入 onAuthStateChanged 方法,那么它会给我:"Maximum call stack size exceeded")。这里是文档:http://ionicframework.com/docs/v2/api/components/tabs/Tab/ - 我理解其中的 90%,但仍然不知道我应该如何传递这个值...
我的第二个问题 - 有没有更好的方法来获取当前用户并将他作为值传递给每个页面?如果我使用 provider 之类的会更好吗?
第三个问题:这个代码在tabs.ts比在app.ts好吗?
谢谢!
您可以在 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/ 它允许您在任何页面之间共享数据。
Provider 是一个不错的选择。
- 视情况而定。更好的方法是在应用程序启动时使用 app.ts 中的提供程序进行一次授权。