Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Angular 2 doesn't load default app-root component?

I'm trying to start Angular app generated via angular CLI, but it seems like the default app-root component doesn't load. Need to say, that I'm using proxy for connection between angular app and express server, and I'm running two scripts at the same time: node bin/www for express/node.js start and ng serve --proxy-config proxy.config.json for starting Angular and creating proxy connection, it looks like this (the part of package.json):

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

The index page loads fine, but it seems that app-root component (the default component, which was created from angular CLI ng new) doesn't loading: enter image description here Here is my node.js/express uses and a route:

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;

And the structure of my project (if needed): enter image description here

What did I miss? Why the default app-root component doesn't loading? (need to say, when I run ng serve, it starts the angular homepage as needed and the component is OK, so I think the problem is somewhere in express).

Thanks in advance

like image 253
Commercial Suicide Avatar asked Jun 13 '17 21:06

Commercial Suicide


People also ask

Why my Angular app not working?

To resolve the problem, you need to import the missing module into your module. Most of the cases, that module would be the AppModule in your app directory. Only import the modules you really need! Importing unnecessarily module bloats your application size significantly.

What is Angular app root?

The root component that Angular creates and inserts into the index. html host web page. The default application created by the Angular CLI only has one component, AppComponent , so it is in both the declarations and the bootstrap arrays.

Can we have multiple root modules in Angular?

The root module is the main module in an Angular application. It is generated by the Angular CLI as AppModule and bootstrapped when the application starts. Every other Angular module depends either directly or indirectly on the root module. Only one root module can exist in an Angular application.

What is AppComponent in Angular?

A bootstrapped component is an entry component that Angular loads into the DOM during the bootstrap process (application launch). Other entry components are loaded dynamically by other means, such as with the router. Angular loads a root AppComponent dynamically because it's listed by type in @NgModule. bootstrap .


1 Answers

You should serve the contents of the dist/ folder after calling ng build --prod (the --prod is important, as the default is --dev). So, it would be something like this:

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

And, more or less adapting your express script:

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'));
});
like image 144
acdcjunior Avatar answered Oct 05 '22 11:10

acdcjunior