6

我已经开发了 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>,然后我在 app.component.ts 中包含了 header.component.ts 的选择器, <app-header></app-header>然后在最后的索引中。我已经包含了 app.component.ts 的选择器,即<app-root></app-root>

你能告诉我是否有什么问题吗?

在问这个问题之前,我已经经历了下面,没有任何帮助

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

3 回答 3

8

在服务器上,我们可以使用 hashStrategy 进行路由。

在路由文件中只需替换RouterModule.forRoot(routes)RouterModule.forRoot(routes,{useHash : true}).

于 2019-12-15T17:53:34.897 回答
0

对于 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 内的基本 href 是

于 2020-07-17T16:27:02.893 回答
0

这可能有点奇怪,但我在cssclass="class class2"中有一个尾随空格

于 2020-09-17T02:47:54.437 回答