Javascript 手动刷新或直接键入url时,角度路由会转到主页
我有一个angular应用程序由node.js服务器提供服务。在手动刷新或直接键入url/xyz或/abc时,如下面的示例所示。我看到它被定向到主页 例:我点击导航按钮(xyz和abc),它会带我到相应的位置。我只看到附加到的/xyz或/abc 我在制作中是有棱角的,我把这个作为静态文件。我将展示一些代码片段。 请基于JWT对用户进行身份验证,它存储在localStorage中 应用程序路由Javascript 手动刷新或直接键入url时,角度路由会转到主页,javascript,node.js,angular,angular-routing,Javascript,Node.js,Angular,Angular Routing,我有一个angular应用程序由node.js服务器提供服务。在手动刷新或直接键入url/xyz或/abc时,如下面的示例所示。我看到它被定向到主页 例:我点击导航按钮(xyz和abc),它会带我到相应的位置。我只看到附加到的/xyz或/abc 我在制作中是有棱角的,我把这个作为静态文件。我将展示一些代码片段。 请基于JWT对用户进行身份验证,它存储在localStorage中 应用程序路由 ..I have imported those components here.. const appR
..I have imported those components here..
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent , canActivate: [AuthGuard]},
{ path: '', component: HomeComponent,canActivate: [AuthGuard],
//{ path: '', component: SigninComponent,
children: [
{ path: '', component: xyz },
{ path: 'xyz', component: xyz },
{ path: 'abc', component: abc },
]}
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes
)
],
exports: [RouterModule],
providers: [],
})
export class RoutingModule { }
app.js
app.use(express.static(path.join(__dirname, '/client')));
app.get('/',function(req,res){
res.sendFile(path.join(__dirname, 'client/index.html'));
});
let index = path.join(__dirname, '../../client/index.html');
res.sendFile(index);
在重定向到index.html时,我希望将我带到应用程序路由。所以,我在中进行手动刷新时,我保留在其自身,而不是主页上
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Portal</title>
<base href="/">
<app-root>
</app-root>
</body>
</html>
门户
如果我理解正确,您当前所在的页面将在刷新时自动重定向
要解决此问题,您需要在路由器中启用哈希路由,如下所示:
imports: [
RouterModule.forRoot(appRoutes, {useHash: true})
],
这将使angular在刷新时不会重定向到基本url您的主组件具有[AuthGuard],请尝试删除AuthGuard并进行检查。我删除了AuthGuard并进行了检查,这是相同的问题。AuthGuard在本地存储中持久化数据时返回true