gpt4 book ai didi

javascript - 带有 Angular 2 rc.5 的 NodeJs 路由目录

转载 作者:太空狗 更新时间:2023-10-29 19:35:26 25 4
gpt4 key购买 nike

我正在处理我的第一个 NodeJs + Angular 2 项目。在我以前的项目中使用这些路由时,它在没有 Angular2 的情况下工作得很好。

我在 NodeJs 中访问多级目录路由时出错。

例子:

它的工作方式是这样的:

app.get('/dashboard', dashboard.getHome);        // works
app.get('/user', user.getUser); // works

代替:

app.get('/dashboard/home', dashboard.getHome);   // won't work
app.get('/user/home', user.getUser); // won't work

我知道可以使用 Angular2 路由,但是是否可以直接访问我在 NodeJs 中指定的静态 URL?

如果我想访问超过 1 个级别,我会收到错误消息:

/dashboard/tasks/42

我现在做不到,这就是我正在努力解决的问题。

应用程序.js

var main = require('./routes/index');
var dashboard = require('./routes/dashboard');
var user = require('./routes/user');

app.get('/', main.index);

app.get('/dashboard', dashboard.getHome); // works
// app.get('/dashboard/home', dashboard.getHome); // won't work this way

app.get('/user', user.getUser); // works
// app.get('/user/home', user.getUser); // won't work this way

仪表板/index.html

<html>
<head>
<title>Teamo - Dashboard</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="javascripts/core-js/client/shim.min.js"></script>
<script src="javascripts/zone.js/dist/zone.js"></script>
<script src="javascripts/reflect-metadata/Reflect.js"></script>
<script src="javascripts/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
<script>
System.import('app/dashboard/dashboard').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-dashboard>Loading...</my-dashboard>
</body>

即使我在 app.js 中设置了以下路由,也无法使用此 URL:

localhost:3000/dashboard/home

它返回以下错误。

错误:

GET http://localhost:3000/dashboard/javascripts/core-js/client/shim.min.js 
GET http://localhost:3000/dashboard/javascripts/zone.js/dist/zone.js
GET http://localhost:3000/dashboard/javascripts/reflect-metadata/Reflect.js
GET http://localhost:3000/dashboard/javascripts/systemjs/dist/system.src.js
GET http://localhost:3000/dashboard/systemjs.config.js
GET http://localhost:3000/dashboard/js/pace.min.js
Uncaught ReferenceError: System is not defined(anonymous function) @ home:18
GET http://localhost:3000/dashboard/js/pace.min.js
favicon.ico:1 GET http://localhost:3000/favicon.ico 500 (Internal Server Error)

最佳答案

改变元素的顺序:

 app.get('/dashboard', dashboard.getHome);        // works
app.get('/dashboard/home', dashboard.getHome); // won't work

到:

   app.get('/dashboard/home', dashboard.getHome);   // won't work
app.get('/dashboard', dashboard.getHome); // works

express 对订单敏感。

关于javascript - 带有 Angular 2 rc.5 的 NodeJs 路由目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968788/

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