How to overwrite a component in Angular 5?

Matteo Calò

I have a component <my-component> in Angular 5

@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
export class MyComponent {

click(param: string){
 console.log(param);
}

And in my html I have something like this:

<my-component (click)="click('Hello world')"></my-component>

I need to overwrite click function to execute: console.log('Param: ' + param);

How can I do this???

Lynx 242

In your app.module.ts

import { OriginalComponent } from './original/original.component';
import { MockOfOriginalComponent } from './mock/mock-of-original.component';

@NgModule({
  declarations: [
    AppComponent,
    WelcomeComponent,
    OriginalComponent,
    MockOfOriginalComponent

  ],
  providers: [],
  imports: [
    BrowserModule,
    HttpClientModule,
    RouterModule.forRoot([
      { path: 'welcome', component: WelcomeComponent },
      { path: 'pm-mock-of-original', component: MockOfOriginalComponent },
      { path: 'pm-original', redirectTo: 'pm-mock-of-original' },
    ]),
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Your OriginalComponent

@Component({
    moduleId: module.id,
    selector: 'pm-original',
    templateUrl: 'original.component.html',
    styleUrls: ['original.component.scss']
    })

    export class OriginalComponent {

    }

Your MockOfOriginalComponent

@Component({
    moduleId: module.id,
    selector: 'pm-mock-of-original',
    templateUrl: 'mock-of-original.component.html',
    styleUrls: ['mock-of-original.component.scss']
    })

    export class MockOfOriginalComponent {

    }

The trick is the redirection in your AppModule

  { path: 'pm-mock-of-original', component: MockOfOriginalComponent },
  { path: 'pm-original', redirectTo: 'pm-mock-of-original' }

I tested this locally and it worked properly. If it doesn't try this

  { path: 'pm-mock-of-original', component: MockOfOriginalComponent },
  { path: 'pm-original', redirectTo: 'pm-mock-of-original', pathMatch: 'full' }

This also works when you import both components from (external) modules.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I overwrite an existing component in angular?

From Dev

Overwrite css class property for library component in Angular-5

From Dev

How can I extend or overwrite component decorators in Angular Material 2?

From Dev

How to overwrite body property in component?

From

how to format date in Component of angular 5

From Dev

How to switch component in angular 5 like template?

From Dev

Can we overwrite existing html component with same name component in Angular?

From Dev

How to overwrite file with angular schematics?

From Dev

How to overwrite specific style in material ui component

From Dev

MaterialUI, how to overwrite styles of nested MUI component?

From Dev

How to overwrite default props of a higher order component?

From Dev

Ionic 2: How to overwrite the style of ionic component

From Dev

Angular 5 -> providers in Component

From Dev

Angular 4+ overwrite child component's style

From Dev

How do I tell which component is currently active Angular 5

From Dev

How to provide service inside the component in angular 5 with aot?

From

How to re-render a component manually Angular 5

From Javascript

How to load dynamic HTML into DIV with component? Angular5

From Javascript

How to inherit css styles in child component from parent in Angular 5

From Dev

How to add external CSS files - styleUrls - Angular 5 component

From Dev

How to declare Inputs on a component in Angular 2 in ES5?

From Dev

How do I inject an AngularJS service in an Angular 5 component?

From Dev

how to add styles to dynamically created component - angular 5

From

Angular 5 and material - How to change the background color from SnackBar component

From

Angular 5 testing: how to get a reference to the child component

From Dev

How to load conditionally templateUrl html file in Angular 5 component

From Dev

How to unit test (with angular 5) a component that depends on the height of an element?

From Dev

How to focus deep nested input from root component in Angular 5?

From Dev

How to style component background-color in angular 4 or 5?

Related Related

  1. 1

    How do I overwrite an existing component in angular?

  2. 2

    Overwrite css class property for library component in Angular-5

  3. 3

    How can I extend or overwrite component decorators in Angular Material 2?

  4. 4

    How to overwrite body property in component?

  5. 5

    how to format date in Component of angular 5

  6. 6

    How to switch component in angular 5 like template?

  7. 7

    Can we overwrite existing html component with same name component in Angular?

  8. 8

    How to overwrite file with angular schematics?

  9. 9

    How to overwrite specific style in material ui component

  10. 10

    MaterialUI, how to overwrite styles of nested MUI component?

  11. 11

    How to overwrite default props of a higher order component?

  12. 12

    Ionic 2: How to overwrite the style of ionic component

  13. 13

    Angular 5 -> providers in Component

  14. 14

    Angular 4+ overwrite child component's style

  15. 15

    How do I tell which component is currently active Angular 5

  16. 16

    How to provide service inside the component in angular 5 with aot?

  17. 17

    How to re-render a component manually Angular 5

  18. 18

    How to load dynamic HTML into DIV with component? Angular5

  19. 19

    How to inherit css styles in child component from parent in Angular 5

  20. 20

    How to add external CSS files - styleUrls - Angular 5 component

  21. 21

    How to declare Inputs on a component in Angular 2 in ES5?

  22. 22

    How do I inject an AngularJS service in an Angular 5 component?

  23. 23

    how to add styles to dynamically created component - angular 5

  24. 24

    Angular 5 and material - How to change the background color from SnackBar component

  25. 25

    Angular 5 testing: how to get a reference to the child component

  26. 26

    How to load conditionally templateUrl html file in Angular 5 component

  27. 27

    How to unit test (with angular 5) a component that depends on the height of an element?

  28. 28

    How to focus deep nested input from root component in Angular 5?

  29. 29

    How to style component background-color in angular 4 or 5?

HotTag

Archive