问题是这样的: 我的 Angular 应用程序通过服务器提供如下服务(它是提供静态 html 文件的服务器) 本地主机:3000/我的应用程序 此 URL 为我的 Angular 应用程序提供了 index.html 和脚本
如何在应用程序内配置路由,以便当我转到 localhost:3000/my-app/first-component 时 特定组件打开了吗?
我当前在 Angular 应用程序中的路线:
export const routes: Routes = [
{
path: "app",
component: FirstComponent
},
{
path: "app/second-component",
component: SecondComponent
}
];
我的express server.js 文件:
const express = require("express");
const path = require('path');
const app = express();
const port = 3000;
app.use('/scripts', express.static(path.join(__dirname, 'scripts')))
app.all('/app', function(req, res, next) {
res.sendFile('index.html', { root: __dirname });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
运行 localhost:3000/app 后,我得到了第一个组件,但是当我尝试转到 localhost:3000/app/second-component 时,它不起作用
您可以将路线的路径定义为:
{ path: "my-app/first-component", title: "Unauthorized", component: FirstComponent }
。
另请查看 Angular 路由指南,因为它提供了大量有关如何使用模块创建嵌套路由的有用信息,当您开始制作更复杂的应用程序时,这非常有用。