angular - angular.router 在离子应用程序中无法按预期工作
问题描述
我在从一页路由到另一页时遇到问题。我有一个登录页面,它是我的应用程序的主页,所以当登录成功时,我会从登录页面导航到个人资料页面。配置文件页面在 ion-router-outlet 中加载,但登录页面不会移开并且始终显示。 AuthService 在登录提交时调用
authenticationState = new BehaviorSubject(false);
constructor(
private msgService: MessagesService,
private http: HTTP,
private router: Router,
private storage: Storage,
private plt: Platform
) {
this.plt.ready().then(() => {
this.checkToken();
});
}
login({ email, password }) {
this.loading = true;
this.http
.post(
'some link for getting token',
{
username: email,
password
},
{ 'Content-Type': 'application/json' }
)
.then(data => {
const res = JSON.parse(data.data);
if (res.status == 'success') {
this.msgService.addMessage('Login successfull', 'success');
console.log(res.data);
this.storage.set('token', res.data.token).then(() => {
this.authenticationState.next(true);
this.router.navigate(['profile']).then(() => {
console.log('going to profile');
});
this.loading = false;
});
} else if (res.status == 'error') {
console.log(res);
this.msgService.addMessage(res.message, 'danger');
this.loading = false;
this.clearRecords();
}
})
.catch(error => {
console.log(error);
this.msgService.addMessage('Internal server error', 'danger');
this.loading = false;
this.clearRecords();
});
}
路由页面.ts
{
path: 'login',
// loadChildren: './pages/auth/login/login.module#LoginPageModule'
loadChildren: () =>
import('./pages/auth/login/login.module').then(m => m.LoginPageModule)
},
{
path: 'register',
loadChildren: './pages/auth/register/register.module#RegisterPageModule'
},
{
path: 'profile',
//canActivate: [AuthGuard],
loadChildren: './pages/profile/profile.module#ProfilePageModule'
},
app.component.ts
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.authService.authenticationState.subscribe(state => {
if (state) {
this.router.navigate(['profile']);
} else {
this.router.navigate(['login']);
}
});
});
}
调试模式下的路由器插座 在 此处输入图像描述
解决方案
推荐阅读
- python - 如何对来自不同文件的不同文本进行聚类?
- kotlin - Kotlin - mutableMapOf() 会保留我输入的顺序
- javascript - 如何在可用时插入元素?
- reactjs - 开玩笑窥探外部功能问题
- sql-server - 我如何从 windows xp 连接到 Sql Server 2017
- sql - Wordpress WooCommerce SQL 查询以通过账单电子邮件和名称获取订单详细信息
- java - jHipster:日期时间的搜索条件“GreaterThan,LessThan”
- c++ - 如何从 C++ 向其他登录用户发送类似“墙”的消息
- merge - 在数据流任务中,如何使用来自另一个源的值限制行流?
- google-apps-script - 如何让用户用多个答案来回答一个问题?