[英]Angular 8 - URL loads with routerLink but doesn't show when directly access in the browser in LOCALHOST
我開發了 Angular 8 應用程序,我正在使用 routerLink 來導航組件,這些組件工作正常,沒有任何問題,但是當我直接在瀏覽器中輸入相同的 URL 時,它沒有顯示任何內容,並且在控制台中,我看到了錯誤如下
例如,我打開主頁http://localhost:4200/home並在此處添加了 routerLink 以轉到http://localhost:4200/about並且我將能夠成功查看但如果我輸入 URL http ://localhost:4200/about直接在 ULR 里什么都沒有顯示
我已經處理了 app-routing.model.ts
const routes: Routes =
[
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
我有 header.component.ts 來處理導航欄,所以我在 header.component.html 中添加了<router-outlet></router-outlet>
然后我在 header.component.ts 中包含了選擇器app.component.ts <app-header></app-header>
然后在最后的 index.html 我已經包含了 app.component.ts 的選擇器,即<app-root></app-root>
你能告訴我是否有問題。
在問這個問題之前,我已經瀏覽了下面的內容,沒有任何幫助
在服務器上,我們可以使用 hashStrategy 進行路由。
在路由文件中,只需將RouterModule.forRoot(routes)
替換為RouterModule.forRoot(routes,{useHash : true})
。
對於 apache 服務器,您可以添加 .htaccess 文件
RewriteEngine On # If an existing asset or directory is requested go to it as it is RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] # If the requested resource doesn't exist, use index.html RewriteRule ^ /index.html
我們必須確保里面的 index.html base href 是
這可能有點奇怪,但我在cssclass="class class2 " 中有一個尾隨空格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.