gpt4 book ai didi

angular - 使用主题标签方法进行路由会破坏应用程序登录

转载 作者:行者123 更新时间:2023-12-04 17:54:38 24 4
gpt4 key购买 nike

我是 Angular 2 的新手,我的路线有以下问题。我的应用程序使用 Auth0 进行用户身份验证,我的主页位于 http://localhost:3000/ 下和 http://localhost:3000/profile 下的个人资料页面.

我遇到的问题是,在刷新个人资料页面时出现无法获取错误 404。我使用散列方法解决了这个错误

RouterModule.forRoot(appRoutes, { useHash: true })

我在这样的帖子中找到的路线:Angular 2 : 404 error occur when i refresh through Browser

我现在的问题是,使用这种哈希方法后,我无法再使用登录名,它会返回一个错误。

EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL >Segment: 'access_token' Error: Cannot match any routes. URL Segment: 'access_token' at ApplyRedirects.noMatchError

所以我想知道我该如何解决这个问题。我的代码:

app.routing.ts

import {ModuleWithProviders} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';

import { HomeComponent } from './components/home/home.component';
import { ProfileComponent } from './components/profile/profile.component';
import { ItemsComponent } from './components/items/items.component';

import {AuthGuard} from './auth.guard';

const appRoutes: Routes= [
{
path:'',
component: HomeComponent

},
{
path:'profile',
component: ProfileComponent,
canActivate: [AuthGuard]
},
{
path:'api/items',
component: ItemsComponent,
canActivate: [AuthGuard]
}

];



export const appRoutingProviders: any[] = [];


export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, { useHash: true });

index.html

 //I skipped copying links and so on

<body>
<base href="/">
<my-app>Loading AppComponent ...</my-app>
</body>

app.component.ts

import { Component } from '@angular/core';
import {Auth} from './services/auth.service';
import {ItemService} from "./services/itemservice/item.service";

@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: 'app.component.html',
providers:[ItemService]
})
export class AppComponent {
constructor(private auth:Auth ){

}

}

app.component.html

 //skipped elements

<nav class="navbar navbar-default ">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ngAuth0</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li ><a routerLink="/">Home</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li ><a href="#" (click)="auth.login()" *ngIf="!auth.authenticated()">Login</a></li>
<li ><a routerLink="/profile" *ngIf="auth.authenticated()">Profile</a></li>
<li ><a href="#" (click)="auth.logout()" *ngIf="auth.authenticated()">Logout</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>

auth.service.ts

import { Injectable }      from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';

// Avoid name not found warnings
declare var Auth0Lock: any;

@Injectable()
export class Auth {
// Configure Auth0
lock = new Auth0Lock('7fZnGMP3H3Sl6aTRPQbLWGwPLeHNlm9z', 'myauthID', {});


constructor() {
// Add callback for lock `authenticated` event


this.lock.on("authenticated", (authResult:any) => {
this.lock.getProfile(authResult.idToken, function(error:any, profile:any){
if(error){
throw new Error(error);
}

localStorage.setItem('id_token', authResult.idToken);
localStorage.setItem('profile', JSON.stringify(profile)); //in localStorage only strings can be stored. stringify is needed

});

});
}

public login() {
// Call the show method to display the widget.
this.lock.show();
}

public authenticated() {
// Check if there's an unexpired JWT
// This searches for an item in localStorage with key == 'id_token'
return tokenNotExpired();
}

最佳答案

问题是,在重新加载时,您实际上是在尝试在您的服务器(后端)中查找“配置文件”文件。您实际上希望所有请求都由 angular2(前端)处理。使用 hashLocationStrategy,您的个人资料页面现在位于 http://localhost:3000/#/profile .

另一个错误是当你有 {path:'', .. } 所以所有的 url 都匹配该路径(因为所有的 url 在开始时都没有"nothing"),所以你必须做到:

{
path:'',
component: HomeComponent,
pathMatch: 'full'
}

我不知道这是否能解决您的问题,但请尝试进行此更改,并搜索如何配置您的 HashLocationStrategy(在 app.module.ts 上很常见)

关于angular - 使用主题标签方法进行路由会破坏应用程序登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41409084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com