首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >浏览器中的Url不会导航到相应的路由器链路

浏览器中的Url不会导航到相应的路由器链路
EN

Stack Overflow用户
提问于 2018-05-27 09:48:58
回答 2查看 374关注 0票数 0

我有一个运行在apache web服务器上的单页网站,使用angular cli编写。我有一个主页和多个链接,使用routerlink标签导航,工作正常。然而,当我点击"about",例如,它导航到website.net/about,一切都是完美的,直到页面重新加载,或者当它抛出404错误时,我手动输入website.net/about。

这是应用程序的主要代码:

index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Title</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule} from '@angular/router';

import { AppComponent } from './app.component';
import { ProgramsComponent } from './programs/programs.component';
import { AboutComponent } from './about/about.component';

@NgModule({
  declarations: [
    AppComponent,
    ProgramsComponent,
    AboutComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot([{
        path: 'programs',
        component: ProgramsComponent
      },
      {
        path: 'about',
        component: AboutComponent
      }
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

import { Component } from '@angular/core';
import {ProgramsComponent} from './programs/programs.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Title';
}

app.component.html:

<div id="header" style="text-align:Left">
  <header>
    <a href="../index.html">
      <h1>
        {{ title }}
      </h1>
    </a>
    <nav>
      <div class="nav-comp">
        <a routerLink="/about">About</a>
        <a routerLink="/programs">Programs</a>
      </div>
    </nav>
  </header>
</div>
<router-outlet></router-outlet>

任何帮助和/或链接将不胜感激,如果这可以使用apache web服务器修复,我会开放的,但我不希望这样做

EN

回答 2

Stack Overflow用户

发布于 2018-05-27 10:50:27

我猜问题出在您的apache配置中,显然您的代码没有任何问题。

票数 0
EN

Stack Overflow用户

发布于 2018-05-29 07:16:47

好的,所以我不得不做一些挖掘,我想在reddit上感谢u/vaskemaskine来获取.htaccess文件,在this post上使用sci buff,但现在我要开始了

因此,首先在/var/www/html中,我必须创建一个名为.htaccess的文件,并将以下代码放入其中

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]

然后为了实现这一点,由于Apache2.4中的一个变化,我不得不通过修改AllowOverrides in /etc/apache2/apache2.conf来实现

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

<Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride All
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

但是,我在运行RewriteEngine时遇到错误,因为它并未安装,但只需在终端中运行sudo a2enmod rewrite && sudo service apache2 restart即可将其清除

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50548397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档