繁体   English   中英

Angular 8 - URL 使用 routerLink 加载,但在 LOCALHOST 的浏览器中直接访问时不显示

[英]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>

你能告诉我是否有问题。

在问这个问题之前,我已经浏览了下面的内容,没有任何帮助

  1. RouterLink 不起作用
  2. Angular 2 路由在部署到 Http 服务器时不起作用
  3. Angular2 路由 - url 不会改变并且页面不会加载

在服务器上,我们可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM