[英]Angular 8 - URL loads with routerLink but doesn't show when directly access in the browser in LOCALHOST
I have developed the Angular 8 application and I am using the routerLink to navigate the components which work fine without any issue but when I enter the same URL directly in the browser it doesn't show anything and in the console, I am seeing the errors as below我开发了 Angular 8 应用程序,我正在使用 routerLink 来导航组件,这些组件工作正常,没有任何问题,但是当我直接在浏览器中输入相同的 URL 时,它没有显示任何内容,并且在控制台中,我看到了错误如下
For Example, I open the homepage http://localhost:4200/home and I have added the routerLink here to go to http://localhost:4200/about and I will be able to view successfully but if I enter the URL http://localhost:4200/about directly in the ULR nothing shows例如,我打开主页http://localhost:4200/home并在此处添加了 routerLink 以转到http://localhost:4200/about并且我将能够成功查看但如果我输入 URL http ://localhost:4200/about直接在 ULR 里什么都没有显示
I have taken care of the app-routing.model.ts我已经处理了 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]
})
And I have header.component.ts to handle the navigation bar so I have added the <router-outlet></router-outlet>
in the header.component.html and then I have included the selector of header.component.ts in app.component.ts <app-header></app-header>
and then in finally index.html I have included the selector of app.component.ts ie <app-root></app-root>
我有 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>
Can you please tell me if there is something wrong.你能告诉我是否有问题。
Before asking this question I have gone through below and nothing helps在问这个问题之前,我已经浏览了下面的内容,没有任何帮助
On server we can use hashStrategy for routing.在服务器上,我们可以使用 hashStrategy 进行路由。
In routing file just replace RouterModule.forRoot(routes)
to RouterModule.forRoot(routes,{useHash : true})
.在路由文件中,只需将
RouterModule.forRoot(routes)
替换为RouterModule.forRoot(routes,{useHash : true})
。
for apache server you can add .htaccess file对于 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
We have to make sure that inside index.html base href is我们必须确保里面的 index.html base href 是
这可能有点奇怪,但我在cssclass="class class2 " 中有一个尾随空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.