[英]Angular routing takes to home page on manual refresh or directly typing url
我有由node.js服務器提供的角度應用程序。 手動刷新或直接輸入url / xyz或/ abc時,如下例所示,我將其定向到主頁。
例如:我點擊導航按鈕(xyz和abc),它將帶我分別到達https:// mywebsite / xyz和https:// mywebsite / abc 。 我只看到/ xyz或/ abc附加到https:// mywebsite 。
我從事生產工作,我將其用作靜態文件。 我將顯示一些代碼片段。 請基於JWT對用戶進行身份驗證,它存儲在localStorage中
應用路由
..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時可以進入應用程序路由。 因此,我在https:// mywebsite / abc中進行手動刷新,但仍留在https:// mywebsite / abc本身而不是主頁中。
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})
],
這將使角度在刷新時不會重定向到基本URL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.