Skip to content
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

Submodules and components #9

Merged
merged 8 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.router';
import { LayoutComponent } from './shared/layout/layout.component';
import { RouterModule } from '@angular/router';
import { SharedModule } from './shared/shared.module';

@NgModule({
declarations: [
AppComponent,
LayoutComponent
],
imports: [
AppRoutingModule,
BrowserModule,
SharedModule,
],
providers: [],
bootstrap: [AppComponent]
Expand Down
21 changes: 21 additions & 0 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LayoutComponent } from './layout/layout.component';
import { TitleComponent } from './title/title.component';
import { RouterModule } from '@angular/router';

@NgModule({
imports: [
CommonModule,
RouterModule,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required if any of your components use router-outlet

],
exports: [
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components this module exports.

If you want to use any of these components in another module you can import SharedModule (this module) in the other module, anything that is exported here will be available in the other end.

LayoutComponent,
TitleComponent,
],
declarations: [
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components declared by this module

LayoutComponent,
TitleComponent,
],
})
export class SharedModule { }
4 changes: 4 additions & 0 deletions src/app/shared/title/title.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h2>{{ message }}</h2>
<hr />
</div>
Empty file.
25 changes: 25 additions & 0 deletions src/app/shared/title/title.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { TitleComponent } from './title.component';

describe('TitleComponent', () => {
let component: TitleComponent;
let fixture: ComponentFixture<TitleComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TitleComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TitleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions src/app/shared/title/title.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component, Input } from '@angular/core';

@Component({
selector: 'app-title',
templateUrl: './title.component.html',
styleUrls: ['./title.component.scss']
})
export class TitleComponent {
@Input()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This decorator allows you to set the value of this field in the html markup.

<app-title message="some message"></app-title>

message: string;
}
17 changes: 17 additions & 0 deletions src/app/tutorials/input/input.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div>
<p>You can use @Input to decorate a field making it accept values sent by the component props.</p>
<p>For example:</p>
<pre>
export class TitleComponent {{ '{' }}
@Input()
message: string;
{{ '}' }}
</pre>

<p>And then you can call the component passing a value for message.</p>
<pre>
&lt;app-title message="Hello world" /&gt;
</pre>

<app-title message="Hello world"></app-title>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component Title has a field named message which is decorated with @Input(). This allows you to add a component and set the value of message.

</div>
Empty file.
27 changes: 27 additions & 0 deletions src/app/tutorials/input/input.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { InputComponent } from './input.component';
import { SharedModule } from '../../shared/shared.module';

describe('InputComponent', () => {
let component: InputComponent;
let fixture: ComponentFixture<InputComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ InputComponent ],
imports: [SharedModule],
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(InputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
9 changes: 9 additions & 0 deletions src/app/tutorials/input/input.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component, Input } from '@angular/core';

@Component({
selector: 'app-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.scss']
})
export class InputComponent {
}
17 changes: 14 additions & 3 deletions src/app/tutorials/tutorials.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<div class="container-fluid">
<div class="Tutorials-container container-fluid">
<fieldset>
<legend>TutorialsComponent</legend>
<div>
Tutorials
<div class="row">
<div class="col-2">
<ul class="Tutorials-menu">
<li>
<a routerLink="/tutorials/input" [routerLinkActive]="'active'">@Input</a>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use routerLinkActive to bind a class name that is going to be set on the element if the current route match the routerLink

</li>
</ul>
</div>
<div class="col-10">
<div class="container-fluid">
<router-outlet></router-outlet>
</div>
</div>
</div>
</fieldset>
</div>
25 changes: 25 additions & 0 deletions src/app/tutorials/tutorials.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.Tutorials-container fieldset {
padding-top: 0;
}

.Tutorials-menu {
background-color: #f1f1f1;
list-style: none;
margin: 0;
padding: 0;
}

.Tutorials-menu li {
margin: 0;
}

.Tutorials-menu a {
display: block;
color: #000;
padding: .5em;
}

.Tutorials-menu a.active {
background-color: #4CAF50;
color: #fff;
}
4 changes: 3 additions & 1 deletion src/app/tutorials/tutorials.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { TutorialsComponent } from './tutorials.component';
import { RouterTestingModule } from '@angular/router/testing';

describe('TutorialsComponent', () => {
let component: TutorialsComponent;
let fixture: ComponentFixture<TutorialsComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TutorialsComponent ]
declarations: [ TutorialsComponent ],
imports: [RouterTestingModule],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test components that use router-outlet you need to import RouterTestingModule, a module specifically for tests.

})
.compileComponents();
}));
Expand Down
5 changes: 4 additions & 1 deletion src/app/tutorials/tutorials.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TutorialsComponent } from './tutorials.component';
import { TutorialsRoutingModule } from './tutorials.router';
import { InputComponent } from './input/input.component';
import { SharedModule } from '../shared/shared.module';

@NgModule({
imports: [
CommonModule,
TutorialsRoutingModule,
SharedModule,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports the TitleComponent used by InputComponent in this module.

],
declarations: [TutorialsComponent]
declarations: [TutorialsComponent, InputComponent],
})
export class TutorialsModule { }
9 changes: 8 additions & 1 deletion src/app/tutorials/tutorials.router.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { TutorialsComponent } from './tutorials.component';
import { InputComponent } from './input/input.component';

const routes: Routes = [
{ path: '', component: TutorialsComponent },
{
path: '',
component: TutorialsComponent,
children: [
{ path: 'input', component: InputComponent },
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sub-module is loaded by the path tutorials , so path here is going to be tutorials/input and will render the component InputComponent

],
},
];

export const TutorialsRoutingModule = RouterModule.forChild(routes);