2017-06-13 20 views
6

我試圖啓動Angular應用程序通過角CLI生成,但它似乎像默認app-root組件不加載。需要說的是,我正在使用代理服務器連接angular應用程序和express,並且我同時運行兩個腳本:用於express/node.js start的node bin/www和用於啓動Angular並創建代理連接的ng serve --proxy-config proxy.config.json看起來像這樣(的package.json的一部分):爲什麼Angular 2不加載默認的應用程序根組件?

"scripts": { 
    "start": "concurrently --kill-others \"node bin/www\" \"ng serve --proxy-config proxy.config.json\"" 
} 

索引頁面加載罰款,但似乎app-root組件(默認組件,這是從角CLI ng new創建)不會加載: enter image description here 這是我的node.js/express use s和路線:

var express = require('express'); 
var router = express.Router(); 
var app = express(); 
var path = require('path'); 

app.use(express.static('./src/client/')); 
app.use(express.static('./')); 
app.use(express.static('./tmp')); 
app.use('/*', express.static(path.resolve('src/client/index.html'))); 

router.get('*', function(req, res) { 
    res.sendFile(path.resolve('src/client/index.html')); 
}); 

module.exports = router; 

我的項目的結構(如果需要): enter image description here

我錯過了什麼?爲什麼默認app-root組件不加載? (需要說的是,當我運行ng serve時,它會根據需要啓動角度主頁,並且組件可以正常運行,所以我認爲問題在某處表達)。

在此先感謝

+0

使用'伍Serve'和快遞服務是重複的。即使如此,你可以從build文件夾中找到,而不是src文件夾。 –

+0

@TaylorAckley,我試圖啓動只是表達和默認情況下使用生成文件夾稱爲「dist」,但仍然有同樣的問題(但現在所有腳本都加載)。 –

+0

@Ced,這裏是我收到的'index.html':https://ibb.co/czJe2k,和我的網絡標籤:https://ibb.co/hE3Jv5 –

回答

5

你就應該全心全意dist/文件夾after calling ng build --prod (the --prod is important, as the default is --dev)的內容。因此,這將是這樣的:

"scripts": { 
    "start": "ng build --prod && node bin/www" 
} 

而且,或多或少地適應您的明確腳本:

app.use(express.static('./dist')); 
app.use('/*', express.static(path.resolve('dist/index.html'))); 

router.get('*', function(req, res) { 
    res.sendFile(path.resolve('dist/index.html')); 
}); 
+0

你的建議看起來不錯,但我試圖使用它現在我得到404錯誤。也許你在某個地方拼錯了,或者忘了在你的回答中加入一些東西?我覺得,你的建議非常接近。需要說的是,我完全是新的節點/快遞 –

+0

哦,是的,我忘了添加'index.html'。讓我知道它是否有效。 – acdcjunior

+0

我試過了,index.html腳本加載正常,但我仍然得到一個空的'app-root'元素。但是我的構建結果('dist'文件夾)只包含'client/index.html'和五個'... bundle.js'文件。所有這些都包含在'index.html'中,但是當我試圖在新選項卡中打開其中的一個時,我沒有看到任何代碼。看起來他們並不是真的包括在內。我認爲,這是另一個問題,但如果你有任何想法,請告訴我 –

相關問題