我目前正在开发angular 2应用程序。但我很难理解为什么我的路由工作在ng serve
而不是在nodemon
?我已经尝试了基于我研究的多种方法,但他们并不像我想象的那样工作。 (可能是由于我缺乏了解)Angular 2路由只适用于ng服务,但不适用于nodemon
方法1
添加在app-routing.module.ts
useHash: true
但是,这会导致所有的URL有#
出现在它。我在想这是为了调试目的吧?
const routes = [
{ path: '', component: sampleComponent1 },
{ path: 'page2', component: sampleComponent2 }
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
方法2
变化app.js
渲染index
每当抛出错误。此方法在URL中不会有#
,但在我的控制台中将始终返回错误,如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是做了正确的道路。有人可以帮我解决这个问题吗?
'useHash'不仅仅用于调试。这是一个设计选择。 – HaveSpacesuit
您是否尝试过构建并使用nodemon服务dist文件夹? – Janpan
@Janpan是的,我会在运行nodemon之前始终做一个构建 – JustStarted