--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | const HtmlWebpackPlugin = require('html-webpack-plugin');
2 |
3 | module.exports = {
4 | entry: './src/main.ts',
5 | module: {
6 | rules: [
7 | {
8 | test: /\.ts$/,
9 | use: ['ts-loader', 'angular2-template-loader'],
10 | exclude: /node_modules/
11 | },
12 | {
13 | test: /\.(html|css)$/,
14 | loader: 'raw-loader'
15 | },
16 | ]
17 | },
18 | resolve: {
19 | extensions: ['.ts', '.js']
20 | },
21 | plugins: [
22 | new HtmlWebpackPlugin({
23 | template: './src/index.html',
24 | filename: 'index.html',
25 | inject: 'body'
26 | })
27 | ],
28 | optimization: {
29 | splitChunks: {
30 | chunks: 'all',
31 | },
32 | runtimeChunk: true
33 | },
34 | devServer: {
35 | historyApiFallback: true
36 | }
37 | };
--------------------------------------------------------------------------------
/src/app/app.component.ts:
--------------------------------------------------------------------------------
1 | import { Component, OnDestroy } from '@angular/core';
2 | import { Subscription } from 'rxjs';
3 |
4 | import { MessageService } from './_services/index';
5 |
6 | @Component({
7 | selector: 'app',
8 | templateUrl: 'app.component.html'
9 | })
10 |
11 | export class AppComponent implements OnDestroy {
12 | messages: any[] = [];
13 | subscription: Subscription;
14 |
15 | constructor(private messageService: MessageService) {
16 | // subscribe to home component messages
17 | this.subscription = this.messageService.getMessage().subscribe(message => {
18 | if (message) {
19 | this.messages.push(message);
20 | } else {
21 | // clear messages when empty message received
22 | this.messages = [];
23 | }
24 | });
25 | }
26 |
27 | ngOnDestroy() {
28 | // unsubscribe to ensure no memory leaks
29 | this.subscription.unsubscribe();
30 | }
31 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2019 Jason Watmore
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-7-communicating-between-components",
3 | "version": "1.0.0",
4 | "repository": {
5 | "type": "git",
6 | "url": "https://github.com/cornflourblue/angular-7-communicating-between-components.git"
7 | },
8 | "scripts": {
9 | "build": "webpack --mode production",
10 | "start": "webpack-dev-server --mode development --open"
11 | },
12 | "license": "MIT",
13 | "dependencies": {
14 | "@angular/common": "^7.0.1",
15 | "@angular/compiler": "^7.0.1",
16 | "@angular/core": "^7.0.1",
17 | "@angular/forms": "^7.0.1",
18 | "@angular/platform-browser": "^7.0.1",
19 | "@angular/platform-browser-dynamic": "^7.0.1",
20 | "@angular/router": "^7.0.1",
21 | "core-js": "^2.5.7",
22 | "rxjs": "^6.3.3",
23 | "zone.js": "^0.8.26"
24 | },
25 | "devDependencies": {
26 | "@types/node": "^10.0.4",
27 | "angular2-template-loader": "^0.6.2",
28 | "html-webpack-plugin": "^3.2.0",
29 | "raw-loader": "^1.0.0",
30 | "ts-loader": "^5.3.3",
31 | "typescript": "^3.3.1",
32 | "webpack": "^4.29.0",
33 | "webpack-cli": "^3.2.1",
34 | "webpack-dev-server": "^3.1.4"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------