Câu hỏi phỏng vấn Angular
Câu hỏi

How to inject base href ?

Câu trả lời

To inject the base href in an Angular application, you can use the APP_BASE_HREF token provided by Angular. This token allows you to configure the base href dynamically during the runtime of your application, rather than statically in the index.html file. Here’s how you can do it:

  1. Import the Necessary Modules:
    First, you need to import APP_BASE_HREF from @angular/common in your module where you want to provide the base href.

    typescript Copy
    import { APP_BASE_HREF } from '@angular/common';
  2. Provide the APP_BASE_HREF in Your Module:
    In the module (typically the app module), you can provide a value for APP_BASE_HREF using Angular's dependency injection framework. You can set this value based on any logic or configuration you have in your application.

    typescript Copy
    @NgModule({
      declarations: [
        // your components here
      ],
      imports: [
        // your modules here
      ],
      providers: [
        { provide: APP_BASE_HREF, useValue: '/your-base-href/' }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

    In this example, /your-base-href/ is the value you want to set as your base href. You can replace it with any path according to your application's needs.

  3. Using the Injected Value:
    If you need to use the base href value in your components or services, you can inject APP_BASE_HREF into your components or services using Angular's constructor injection.

    typescript Copy
    import { Component, Inject } from '@angular/core';
    import { APP_BASE_HREF } from '@angular/common';
    
    @Component({
      selector: 'app-root',
      template: `<p>Base HREF: {{baseHref}}</p>`
    })
    export class AppComponent {
      constructor(@Inject(APP_BA...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Explain the difference between Constructor and ngOnInit

junior

What is the equivalent of ngShow and ngHide in Angular?

middle

Do I always need a Routing Module?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào