我正在开发角度2应用程序。但是我很难理解为什么我的路由工作在ng serve
而不是nodemon
?我已经尝试了基于我研究的多种方法,但它们并没有真正像我认为的那样工作。 (可能是因为我的理解不好)
方法1
在useHash: true
中添加app-routing.module.ts
但这会导致所有网址中都显示#
。我认为这是出于调试的目的吗?
const routes = [
{ path: '', component: sampleComponent1 },
{ path: 'page2', component: sampleComponent2 }
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
方法2
每当引发错误时,将app.js
更改为呈现index
。此方法在网址中不会有#
,但在我的控制台中始终会返回GET /page2 404 1.688 ms - 987
之类的错误。
app.use(function (err, req, res, next) {
// set locals, only providing error in development
console.log(err.message);
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
到目前为止,我更多地关注方法2,因为URL中没有显示#
,但我不确定方法2是否是正确的方法。有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
使用Angular CLI进行开发的原因是所有不存在的路由都可以作为index.html使用
这允许您的Angular应用程序处理所有路由,而不管实际请求的URL。一旦您的应用程序在浏览器中启动,Angular路由器(假设您正在使用的内容)将读取路由信息并向您的用户显示正确的路由。
此处还有其他答案可以解释此解决方案,具体取决于您的网络服务器。