-
Notifications
You must be signed in to change notification settings - Fork 24.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] Angular2 and app modularity #5063
Comments
If you're using System.js you can map an alias to a particular component/directory. With the directory tree:
Lets say you want to add aliases for:
config.js
So your imports would be:
Alternatively, you could do: config.js
So your imports would be:
In addition, if you install a component from an external repository/package-manager via JSPM. The aliases will automatically be mapped to the the correct location within the /jspm_packages folder |
@evanplaice This does not answer the final part of @artaommahe question:
Which is also a problem i am running into immediately: http://stackoverflow.com/questions/33584763/hot-to-avoid-declaring-all-services-in-the-bootstrap-function It all comes down to the fact that everything has to be within the bootstrap function. This also becomes really annoying when you want to share packages. Lets say i build a module, that module has 10 services those are all nested one below the other or something. The person who grabs my package has to include each and every one of those Classes within the import of his "app"(base) component and add them to the bootstrap function as well. Please correct me if i am wrong about it, because this change breaks the way i used to work with Angular1 in which i saw each service as a Singleton that i used throughout my controllers / other services and even sometimes project specific directives. While this is still possible i would now be forced to specify all those services within the bootstrap method to make them available application wide. |
@schippie Just create // smth-module/index.js
import { SmthFactory } from './smth.factory';
import { SmthService } from './smth.service';
export const SMTH_MODULE = [
SmthFactory,
SmthService
];
// app.js
import { SMTH_MODULE } from './smth-module';
// other imports...
bootstrap(AppComponent, [SMTH_MODULE /*, other providers */]); |
@alexpods Okay, I got the same answer on stackoverflow the with Core directives example. I guess a good way to deal with it is make a base file for each directory (model/ , utils/ , components/) in which you specify this all Thank you 👍 |
@schippie IMHO, I think it's better to have only one base file for a module. This file is a single public interface of your module. // smth-module/index.js
import { SmthFactory } from './smth.factory';
import { SmthService } from './smth.service';
import { SmthComponent } from './smth.component';
import { SmthDirective } from './smth.directive';
export { SmthDirective }
export const SMTH_MODULE_PROVIDERS = [
SmthFactory,
SmthService,
// ...
];
export const SMTH_MODULE_DIRECTIVES = [
SmthComponent,
SmthDirective,
// ...
]
// 'app-component.js'
import { Component } from 'angular2/core';
import { SmthDirective } from '../smth-module';
// or import { SMTH_MODULE_DIRECTIVES } from '../smth-module';
@Component({
// ...
directives: [SmthDirective], // or directives: [SMTH_MODULE_DIRECTIVES]
template: `<smth-directive></smth-directive>`
})
export class AppComponent() {}
// 'app.js'
import { SMTH_MODULE_PROVIDERS } from '../smth-module';
import { AppComponent } from './app-component.js';
bootstrap(AppComponent, [SMTH_MODULE_PROVIDERS /*, other providers */]); |
@alexpods Oh no no, my situation that I was describing at the end with the sub-directories had more to do with the fact: That within a single project you could run into large file base as well. At that point since its purely for a project (non public) it would be the way I would handle it. For modules that I or anybody would write, having a single file is the only correct way to prevent confusion. |
Ok.. i decied to try again. Test app sctructure
// bootstrap.ts
...
import {PageCmp} from 'page-module/index';
bootstrap(PageCmp, [
...
]); // page.ts
...
import {LineGraphCmp} from 'graph-module/index';
...
export class PageCmp {} // line-graph.ts
...
export class LineGraphCmp {} // graph and page modules indexes like this
export {PageCmp} from './component/page/page'; // also added to System.config()
map: {
'graph-module': '/graph-module',
'page-module': '/page-module'
} So i can use
How can i tell Typescript compiler about this modules? Or can i achive this modules usage via other way or modules system? |
this issue solves my problem(when Type script will be updated) |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Angular1 allows to build flexible modular app structure, e.x.(part of current project)
where each module contains directives, services and any other things(e.x. react components). Also each module has index.js file where angular module is declared and directives/services assigned. And this module uses as dependency for another.
So we declare all module items in this module, add it as dependency for top level module and can use this module directives/services anywhere in our app.
Bun what with this thing in Angular2? I tried to build small side project with alpha45 and can't find anything about modular structure. For simple two modules
if i want to use chat component or service in main, i need to import it by whole relative path
and so with each component from another module. If modules will be reallocated, i must fix each import to new relative path.
Also if service requires smth like Http
it's necessary to inject HTTP_PROVIDERS services on top level of app(
bootstrap.js
) or for each component that uses this service(or depend on components inheritance).Again this thing breaks modular structure - components/services from ona module knows about path to components/services from other modules and must watch their dependencies. And this easily solves via modules system of Angular1.
Am i missed something or there is any other thing in plans for this cases? I don't see comfortable use of current ng2 app structure anythere except small project without need of modulaity. Thx )
The text was updated successfully, but these errors were encountered: