2017-06-13 81 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'中,但是当我试图在新选项卡中打开其中的一个时,我没有看到任何代码。看起来他们并不是真的包括在内。我认为,这是另一个问题,但如果你有任何想法,请告诉我 –

相关问题