我正在使用最新的AWS Amplify的身份验证组件.它可以成功登录,但在登录后,我需要将路由发送到另一个我无法实现的url,它在登录后保持相同的url.但我需要设置一个自定义url,如果用户成功登录,它会自动重定向.

注意:我没有使用aws-amplify-angular个软件包,我使用的是这些软件包,

 "@aws-amplify/ui-angular": "^2.4.4", 
 "aws-amplify": "^4.3.21",

我也判断了这个import {AuthenticatorService} from '@aws-amplify/ui-angular';服务,但在这里我没有发现任何可观察类型的响应,我想这就是为什么我没有在用户成功登录后立即得到任何事件或东西.我需要在成功登录后立即发送.所以我需要一个活动,这样我才能做到.

我的main.ts:

import { Amplify } from 'aws-amplify'
import awsmobile from './aws-exports'
Amplify.configure(awsmobile)

auth.component.html:[ts中没有代码]

<amplify-authenticator [signUpAttributes]="['email']"></amplify-authenticator>

&amp;路由是这样设置的,

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'auth',
    component: AuthComponent
  },
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  }
];

使用这个软件包,我没有得到任何好的解决方案.请帮助解决这个问题,否则我的配置中会遗漏什么.

推荐答案

我不想使用aws-amplify-angular软件包,但我需要,因为没有好的解决方案,没有这个软件包,我try 使用Angular 挂钩,因为当变化检测运行时,我可以得到authenticated&;AuthenticatorService个州的unauthenticated个州来自import { AuthenticatorService } from '@aws-amplify/ui-angular';个州,但使用钩子(AfterContentCheckedAfterViewChecked)有时有效,有时无效&;有时会发生错误.

[我没有同时使用两个钩子,我分别try 了]

所以,最后我需要安装aws-amplify-angular以使用AmplifyService的&;然后我就这么做了,

 constructor(private router: Router, private amplifyService: AmplifyService) { }

  ngOnInit() {
    this.amplifyService.authStateChange$.subscribe(({state, user}) => {
      if (state === 'signedIn') {
        this.router.navigate(['/home'])
      }
    })
  }

别忘了在app.module.ts中加入AmplifyService.

Javascript相关问答推荐

空的结果抓取网站与Fetch和Cheerio

从Node JS将对象数组中的数据插入Postgres表

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

如何控制Reaction路由加载器中的错误状态?

确保函数签名中的类型安全:匹配值

检索相加到点的子项

查询参数中的JAVASCRIPT/REACT中的括号

如何在DYGRAPS中更改鼠标事件和键盘输入

面对代码中的错误作为前端与后端的集成

为什么可选参数的顺序会导致问题?

使用Reaction窗体挂钩注册日历组件

MUI迷你图:如何将$符号添加到MUI迷你图中的工具提示数据

如何在Java脚本中并行运行for或任意循环的每次迭代

每隔一行文本段落进行镜像(Boustrophedon)

使用jQuery每隔几秒钟突出显示列表中的不同单词

类型脚本类型声明未按预期工作

如何创建在<;span>;中附加每个字符的自定义元素?

无法从fixture 文件中提取数据

改进了正则表达式的性能

如何 Select 名称正在更改的DOM元素