2016-08-16 59 views
3

我正在開發第一個NodeJs + Angular 2項目。在我以前使用這些路線的項目中,沒有使用Angular2,它工作得很好。使用Angular 2的NodeJs路由目錄rc.5

我在訪問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?

/dashboard/tasks/42 

我不能現在就這樣做,這就是我與掙扎的問題:

,如果我想獲得超過1級就像我會得到錯誤。

app.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:

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) 

回答

0

更改元素的順序:

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是對順序敏感。

+0

對不起,也許我不清楚我的問題。我用更多的細節編輯了我的問題。我實際面臨的問題是我無法擁有超過1級的路線,例如我只能通過'localhost:3000/dashboard'而不是'localhost:3000/dashboard/tasks/42'訪問路線。 我無法訪問比'localhost:3000/dashboard'更深的內容。 – Danzeeeee

+0

@Danzeeeee但你是否嘗試過改變元素的順序,以便在子目錄之後設置父目錄? – 2016-08-16 07:35:22

+0

是的,我試過但沒有運氣。 – Danzeeeee