gpt4 book ai didi

angular - ng 构建后只有主页在 Angular 站点上有效

转载 作者:太空狗 更新时间:2023-10-29 18:27:31 25 4
gpt4 key购买 nike

我正在开发 Angular 8 网站。该站点有多个页面,www.site.com、www.site.com/foo 和 www.site.com/bar。当我使用 ng serve 时,一切正常。但是,当我运行 ng build、ng build --prod 或 ng build --aot 时,唯一可用的页面是根页面。 www.mywebsite.com 可以使用,但 www.mywebsite.com/foo 和 www.mywebsite.com/bar 是一个白屏,显示“找不到文件”。

我已经尝试使用 ng build、ng build --prod 和 ng build --aot 进行构建。

我也尝试按照此 When running ng build, the index.html does nothing? 中的说明进行操作

这是我的 app-routing.module.ts 中的一些路由

const routes: Routes = [
{ path: '', component: HomepageComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'editorial', component: EditorialArticleComponent},
{ path: 'article/:id', component: ArticleComponent },
];

更新:我在 Ubuntu 服务器上使用 nginx

最佳答案

这可以通过设置 HashLocationStrategy 来完成,在根模块中,您需要在之后的提供者列表中将 LocationStrategy 设置为 HashLocationStrategy更改 url 将如下所示 www.mywebsite.com/#/

{
providers : [
{ provide: LocationStrategy, useClass: HashLocationStrategy },
]
}

demo 🔥🔥

还要检查这个 👉 server url rewrite

关于angular - ng 构建后只有主页在 Angular 站点上有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com