创建一个只在首次打开应用时出现的页面

huangapple go评论78阅读模式
英文:

Making a page that appears only upon first time opening the app

问题

以下是翻译好的代码部分:

在我的应用中,我希望用户在启动应用时看到欢迎页面,然后单击页面上的按钮以转到登录并继续使用应用程序... 下次打开应用时,应用程序不会显示启动屏幕,而是直接转到登录或主屏幕... 我已经创建了一个存储服务,用于保存页面是否已经被查看的状态,还有一个页面守卫,根据该状态进行重定向...

但现在我遇到了一个问题,我的应用程序总是跳过启动页面,直接转到登录页面,当我尝试登录时,它停留在这个页面... console.log 显示状态已设置为 'done'...

如果有人能找出我做错了什么,以下是我的代码...

app-routing.module.ts:

const routes: Routes = [
  {
    path: 'login',
    loadChildren: () => import('./login/login.module').then(m => m.LoginPageModule),
    canLoad: [AutoLoginGuard]
  },
  {
    path: '',
    redirectTo: 'startup',
    pathMatch: 'full'
  },
  {
    path: 'startup',
    loadChildren: () => import('./pages/startup/startup.module').then(m => m.StartupPageModule),
    canActivate:[FirstLoadGuard]
  },
  {
    path: 'home',
    loadChildren: () => import('./home/home.module').then(m => m.HomePageModule),
    canLoad: [AuthGuard],
  },
];

storage.service.ts的代码如下:

export class StorageService {
  constructor(private storage: Storage) {
    this.initStorage();
    console.log('Init storage');
  }

  async initStorage() {
    await this.storage.create();
    console.log('Storage ustvarjen');
  }

  async setValue(key, value) {
    await this.storage.set(key, value);
    return true;
  }

  async getValue(key) {
    return await this storage.get(key);
  }
}

first-load.guard.ts的代码如下:

export class FirstLoadGuard implements CanActivate {
  constructor(
    private storageService: StorageService,
    private router: Router
  ) {}
  canActivate(route: ActivatedRouteSnapshot):  Promise<boolean>
  {
    return new Promise(resolve => {
      this.storageService.getValue('first_time').then((value) => {
        if (value !== null) {
          this.router.navigateByUrl('/login');
          console.log('guard if value: ', value);
          resolve(false);
        }
        else {
          this.storageService.setValue('first_time', 'done');
          resolve(true);
        }
      });
    });
  }
}

这是你提供的代码的中文翻译。如果需要更多的帮助或代码,请随时提出。

英文:

In my app I want the user to see this startup/welcome page when the app launches and then click a button on it to go to login and continue using the app… and next time he opens the app the app doesn't show the startup screen and goes straight to the login or home screen... I have made a storage service that saves a state if the page has been seen and a page guard that redirects based on the state...

But now I have an issue that my app keeps skipping the startup page and goes straight to login and when I try to log in it stays on this page… the console.log show that the state is set to 'done'…

here is my code if anyone can figure out what I did wrong...

app-routing.module.ts:

const routes: Routes = [
{
path: &#39;login&#39;,
loadChildren: () =&gt; import(&#39;./login/login.module&#39;).then( m =&gt; m.LoginPageModule),
canLoad: [AutoLoginGuard]
},
{
path: &#39;&#39;,
redirectTo: &#39;startup&#39;,
pathMatch: &#39;full&#39;
},
{
path: &#39;startup&#39;,
loadChildren: () =&gt; import(&#39;./pages/startup/startup.module&#39;).then( m =&gt; m.StartupPageModule),
canActivate:[FirstLoadGuard]
},
{
path: &#39;home&#39;,
loadChildren: () =&gt; import(&#39;./home/home.module&#39;).then( m =&gt; m.HomePageModule),
canLoad: [AuthGuard],
},
];

this is the code of storage.service.ts:

export class StorageService {
constructor(private storage: Storage) {
this.initStorage();
console.log(&#39;Init storage&#39;);
}
async initStorage() {
await this.storage.create();
console.log(&#39;Storage ustvarjen&#39;);
}
async setValue(key,value) {
await this.storage.set(key, value);
return true;
}
async getValue(key) {
return await this.storage.get(key);
}
}

this is the first-load.guard.ts:

export class FirstLoadGuard implements CanActivate {
constructor(
private storageService: StorageService,
private router: Router
) {}
canActivate(route: ActivatedRouteSnapshot):  Promise&lt;boolean&gt;
{
return new Promise(resolve =&gt; {
this.storageService.getValue(&#39;first_time&#39;).then((value) =&gt; {
if (value !== null) {
this.router.navigateByUrl(&#39;/login&#39;);
console.log(&#39;guard if value: &#39;, value);
resolve(false);
}
else {
this.storageService.setValue(&#39;first_time&#39;, &#39;done&#39;);
resolve(true);
}
});
});
}
}

If I have to provide more code, feel free to comment bellow and I'll add more... I trully don't know where my mistake is 创建一个只在首次打开应用时出现的页面

答案1

得分: 1

You can use canMatch to check on the same patch, and you don't need to do redirects

  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  },
  {
    path: 'home',
    loadChildren: () => import('./pages/startup/startup.module').then(m => m.StartupPageModule),
    canMatch: [() => localStorage.getItem('first_time') === null],
  },
  {
    path: 'home',
    loadChildren: () => import('./home/home.module').then(m => m.HomePageModule),
    canLoad: [AuthGuard],
  },
英文:

You can use canMatch to check on the same patch, and you don't need to do redirects

  {
    path: &#39;&#39;,
    redirectTo: &#39;home&#39;,
    pathMatch: &#39;full&#39;
  },
  {
    path: &#39;home&#39;,
    loadChildren: () =&gt; import(&#39;./pages/startup/startup.module&#39;).then( m =&gt; m.StartupPageModule),
    canMatch: [() =&gt; localStorage.getItem(&#39;first_time&#39;) === null],
  },
  {
    path: &#39;home&#39;,
    loadChildren: () =&gt; import(&#39;./home/home.module&#39;).then( m =&gt; m.HomePageModule),
    canLoad: [AuthGuard],
  },

huangapple
  • 本文由 发表于 2023年4月4日 15:17:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定