- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 ionic 3 应用程序中创建了登录和注册功能,它也能正常工作,但登录后的用户名没有立即在边栏中更新,这意味着我需要刷新浏览器,或者我必须关闭应用程序并在这之后重新打开应用程序用户名显示在侧边栏中。但是我想在登录后显示用户名 instanlty 以刷新我在登录后添加加载程序但不起作用的页面。我查看了 ionic 3 中的事件 api,但不知道如何使用。请帮忙。下面是我的 login.ts、app.component.ts 和 app.html
登录.ts
import { Component } from '@angular/core';
import { IonicPage,Events, NavController, LoadingController NavParams,ToastController } from 'ionic-angular';
import { SignupPage } from '../signup/signup';
//import { HomePage } from '../home/home';
import {
AuthServiceProvider
} from "../../providers/auth-service/auth-service";
/**
* Generated class for the LoginPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
loading:any;
responseData : any;
userData = {"user":"","password":""};
constructor(public loadingCtrl: LoadingController,public events: Events,private toastCtrl: ToastController,public navCtrl: NavController, public navParams: NavParams,public authService:AuthServiceProvider) {
//this.title = this.navParams.get('title');
}
login(){
var userMe = '';
for(var key in this.userData) {
if(key) {
userMe = userMe + key + '=' + this.userData[key] + '&';
}
// console.log(this.userData[key]);
}
this.authService.postData(userMe,'user/login').then((result) => {
this.responseData = result;
//console.log(this.responseData);
if(this.responseData.status == 1){
this.loading = this.loadingCtrl.create({
content: 'Waiting...'
});
this.loading.present();
//console.log(this.responseData);
localStorage.setItem('userStorage', JSON.stringify(this.responseData));
this.navCtrl.popToRoot();
this.loading.dismiss();
///this.navCtrl.push(TabsPage);
}
else{
let toast = this.toastCtrl.create({
message: this.responseData.message,
duration: 3000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}, (err) => {
// Error log
});
}
pushSignup(){
this.navCtrl.push(SignupPage);
}
/* pushHome(){
this.navCtrl.push(HomePage);
}*/
ionViewDidLoad() {
console.log('ionViewDidLoad LoginPage');
}
}
应用程序组件.ts
// Angular
import { Component, ViewChild } from '@angular/core';
// RxJS
import { ReplaySubject } from "rxjs/ReplaySubject";
//import { ArrayObservable } from "rxjs/observable/ArrayObservable";
// Ionic
import { Nav,Events, Platform,NavController,LoadingController, MenuController } from 'ionic-angular';
import { AuthServiceProvider } from '../providers/auth-service/auth-service';
// Ionic Native
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
// Pages
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
// Side Menu Component
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild('myNav') nav: NavController
// Get the instance to call the public methods
public rootPage: any = HomePage;
loading: any;
userStorage: any;
hideMe:any;
showMe = false;
showButton : any=false;
// Options to show in the SideMenuComponent
private unreadCountObservable: any = new ReplaySubject<number>(0);
constructor(public authService:AuthServiceProvider ,
private platform: Platform,
private statusBar: StatusBar,
private splashScreen: SplashScreen,
private event: Events,
private menuCtrl: MenuController,
public loadingCtrl: LoadingController) {
this.initializeApp();
this.profileName();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleLightContent();
this.splashScreen.hide();
});
// Change the value for the batch every 5 seconds
setInterval(() => {
this.unreadCountObservable.next(Math.floor(Math.random() * 10));
}, 5000);
}
openMenu() {
this.menuCtrl.open();
}
closeMenu() {
this.menuCtrl.close();
}
toggleMenu() {
this.menuCtrl.toggle();
}
login(){
this.nav.push(LoginPage);
}
logout() {
this.loading = this.loadingCtrl.create({
content: 'Waiting...'
});
this.loading.present();
localStorage.clear();
this.showMe = false;
this.loading.dismiss();
}
profileName(){
if (this.userStorage = JSON.parse(localStorage.getItem('userStorage'))) {
this.showMe = true;
this.showButton = true;
}
else{
this.showMe = false;
}
}
}
侧边栏的 app.html
<ion-menu [content]="myNav">
<ion-content>
<div class="item item-avatar" style="padding: 25px;border-bottom: 1px solid #ccc;">
<img src="//ionicframework.com/img/docs/spengler.jpg">
<h5 *ngIf="showMe == true" class="after-login">{{userStorage.data.surname}}</h5>
<!-- <h6 *ngIf="showMe == true" class="before-login">Please Login.</h6> -->
<!-- <p>Nine Inch Nails</p> -->
</div>
<ion-item *ngIf="showMe == false" menuClose detail-none (click)="login()">Login</ion-item>
<ion-item menuClose detail-none (click)="">My Business</ion-item>
<ion-item menuClose detail-none (click)="">Package</ion-item>
<ion-item menuClose detail-none (click)="">Wallet</ion-item>
<ion-item menuClose detail-none (click)="">Invite Friends</ion-item>
<ion-item menuClose detail-none (click)="">Profile</ion-item>
<ion-item menuClose detail-none (click)=""> FAQ</ion-item>
<ion-item *ngIf="showMe == true" menuClose detail-none (click)="logout()">Logout</ion-item>
</ion-content>
</ion-menu>
<ion-nav #myNav [root]="rootPage"></ion-nav>
最佳答案
您可以使用 Events API
Events is a publish-subscribe style event system for sending and responding to application-level events across your app.
import { Events } from 'ionic-angular';
// login.ts page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
console.log('User created!')
this.events.publish('user:created', user, Date.now());
}
// app.component.ts page (listen for the user created event after function is called)
constructor(public events: Events) {
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);
});
}
在一个组件中,您向 events.publish()
提供数据,然后在另一个组件中,您使用 events.subscribe()
访问该数据。在上面的示例中,数据是 user
和 time
。
在你的情况下
this._events.publish('user:signedIn',
this.userData =
localStorage.setItem('userStorage', JSON.stringify(this.responseData)
);
然后
_events.subscribe('user:signedIn', (userEventData) => {
this.userData = localStorage.getItem('userStorage');
});
关于javascript - 如何在不重新打开应用程序或在浏览器中刷新的情况下登录 ionic 3 后更新侧边栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49392055/
我想在聚焦时使用 ion-input 更改 ion-item 内的 ion-label 颜色。 我可以使用 --highlight-color-focused: yellow; 更改 ion-item
我想用 ionic 3 ion-list (或任何在 Ionic 3 中有效的东西)来显示水平列表而不是典型的垂直列表。 寻找解决方案 没有大量的 css 或难以维护的代码。
我目前使用 ionic 和我的 cordova 插件同时开发一个应用程序。我想知道如何使用文件 plugins/fetch.json 从本地文件系统更新我的插件。有什么想法吗? 最佳答案 使用 ion
当我在 ionic 项目文件夹中运行 ionic link 命令时,我收到以下错误消息 “除非您在 Ionic 项目文件夹中,否则您无法运行此命令” 我的系统信息。 Cordova CLI:6.3.1
如何注释 ion-row这样它就可以填满剩余的空间? 对于以下示例,黄色行“内容”应展开,直到占用所有剩余(绿色)空间。 Example text
我正在尝试构建一个 ionic 应用程序,但是当我运行 npm run ionic:build -–prod 时,出现以下错误: npm run ionic:build -–prod npm ERR!
我想在 ion-footer-bar 中添加两个按钮,如图片,但我的代码无法正常工作。 Button 333333333 Button
请有人澄清一下我什么时候会使用 ion-nav-view反对 ion-view ?我正在学习 AngularJS/Ionic(我对 AngularJS 有基本的了解;并且想使用 Ionic 来增强它)
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 1年前关闭。 Improve t
item-avatar 在我的项目中不起作用,它根本不显示 item-avatar 元素。 Recent Conversations
我正在使用带有属性interface="popover"的ion-select。弹出窗口在 select 下方打开,这使得弹出窗口非常小。 我发现,如果 ion-select 中有 10 个或更多项目
我创建了一个带有电容器的新项目 ionic。我使用 ionic 选择,但我有一个新的 ionic 选择选项,带有大文本,而不是在输入中分布。 Plaga/Enfermedad Al
如何像 ionic 中的许多应用程序一样滑动切换段?我在 ionic 官方文档中找不到任何 api。我只能找到this有用的线程。 但它似乎并不完美。有更好的解决办法吗? ionic 信息: Cord
我正在开发一个登录表单,所以在我的 中我有一个 (用作登录表单的容器)我想垂直居中。我用 css flexbox 和其他 css 技巧尝试了不同的方法,但对我没有任何作用!该卡片保留在页面顶部。你
我想在 ionic 选择(组合框)更改时隐藏和显示文本框 例如我有: ionic 选择中的 1 和 2如果我选择 1 文本框将隐藏,如果我选择 2 文本框将出现 这是我当前的代码: .ts onCha
我尝试在 ionic 4 中使用 ion-button 实现一个按钮,但没有样式输出,问题出在哪里,求助。 最佳答案 请使用以下代码 这里是 ionic v4 按钮的文档。 https://beta
我有一个在 ionic serve 中运行良好的应用程序。我现在正在尝试创建一个构建——这通常有效,但今天我遇到了问题。 ionic package build ios --profile devel
我正在使用 Ionic 2,在我的应用程序中我正在创建一个表单,如果出现验证错误,信息图标将出现在相关输入字段的右侧。 HTML如下,
我正在与一个远程团队一起开发 Ionic 1 应用程序,最近我们的版本彼此不喜欢。我想知道我是否也一直在从事 Ionic 2 项目,无论出于何种原因,我的 CLI “认为”这些应用程序也是 Ionic
我需要禁用默认 ion-ripple-effect在“ ionic 按钮”中。 我无法禁用 pointer-events因为我需要它。 PS:我引用了以下帖子,但找不到适合 Ionic 4
我是一名优秀的程序员,十分优秀!