Angularjs 如果用户已登录,则重定向用户
我想重定向已登录的用户,如果恢复到我的应用程序,将重定向到主页而不是登录页面 在我的登录功能中,如下所示Angularjs 如果用户已登录,则重定向用户,angularjs,ionic-framework,ionic4,Angularjs,Ionic Framework,Ionic4,我想重定向已登录的用户,如果恢复到我的应用程序,将重定向到主页而不是登录页面 在我的登录功能中,如下所示 login(){ this.googlePlus.login({ 'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com', 'offile' : true }).then(res=>{ firebase.aut
login(){
this.googlePlus.login({
'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com',
'offile' : true
}).then(res=>{
firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(res.idToken))
.then(suc=>{
// console.log('users', JSON.stringify(suc));
localStorage.setItem('user', JSON.stringify(suc));
this.router.navigate(["/tabs/home"]);
}).catch(ns=>{
alert('Unsucessful');
})
})
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.overlaysWebView(true);
this.statusBar.show();
firebase.auth().onAuthStateChanged(function(user) {
if (!user) {
console.log("user is not logged in");
} else {
console.log("user is logged in");
this.router.navigateByUrl('/home');
return;
}
});
});
}
我的localStorage中有用户数据,我想我可以检查localStorage上是否存在用户令牌。但是当我在我的登录页面上尝试这个时
ionViewWillEnter() {
firebase.auth().onAuthStateChanged(function(user) {
if (!user) {
console.log("user is not logged in");
} else {
console.log("user is logged in");
this.router.navigateByUrl('/tabs/home');
return;
}
});
}
它没有重定向到主页,有人知道怎么做吗
在尝试像这样插入到app.component.ts我的代码之后
login(){
this.googlePlus.login({
'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com',
'offile' : true
}).then(res=>{
firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(res.idToken))
.then(suc=>{
// console.log('users', JSON.stringify(suc));
localStorage.setItem('user', JSON.stringify(suc));
this.router.navigate(["/tabs/home"]);
}).catch(ns=>{
alert('Unsucessful');
})
})
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.overlaysWebView(true);
this.statusBar.show();
firebase.auth().onAuthStateChanged(function(user) {
if (!user) {
console.log("user is not logged in");
} else {
console.log("user is logged in");
this.router.navigateByUrl('/home');
return;
}
});
});
}
上面的代码给了我这个错误
这是我的app-routing.module
const routes: Routes = [
{
path: '',
loadChildren: () => import('./tabs/tabs.module').then(m => m.TabsPageModule)
},
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' },
{ path: 'welcome', loadChildren: './welcome/welcome.module#WelcomePageModule' },
{ path: 'settings', loadChildren: './settings/settings.module#SettingsPageModule' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
尝试将其放入
app.component.ts
在constractor(){这里是您的代码}我编辑我的问题后,我将其添加到app.component.ts,就像这样,但我无法读取我的控制台上未定义的属性'navigateByUrl'。您的路由器有问题,请检查页面名称或使用此.router.navigate(['../home']))
您的功能现在正在运行,因为他们找到用户并尝试回家,但他无法检查您的路由器模块、页面名称,../或/根据您的页面位置。将路由器更改为private:构造函数(private router:router,…)
我正在使用private router:router,但仍然会出现该错误