首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

为什么在使用angular中的route.navigate进行路由时,父组件中注入的服务不会传递给子组件

在使用Angular中的route.navigate进行路由时,父组件中注入的服务不会自动传递给子组件。这是因为路由导航会销毁当前组件并创建一个新的组件实例,而不是简单地切换组件的视图。

要在父组件中注入的服务能够在子组件中使用,可以通过以下几种方式实现:

  1. 使用@Input装饰器:在父组件中将服务实例作为属性传递给子组件,并使用@Input装饰器将其标记为输入属性。子组件可以通过这个输入属性来访问父组件中的服务实例。

父组件:

代码语言:txt
复制
import { Component } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-parent',
  template: `
    <app-child [myService]="myService"></app-child>
  `,
  providers: [MyService]
})
export class ParentComponent {
  myService: MyService;

  constructor() {
    this.myService = new MyService();
  }
}

子组件:

代码语言:txt
复制
import { Component, Input } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-child',
  template: `
    <p>{{ myService.getData() }}</p>
  `
})
export class ChildComponent {
  @Input() myService: MyService;
}
  1. 使用@ViewChild装饰器:在父组件中使用@ViewChild装饰器获取子组件的实例,并手动将父组件中的服务实例赋值给子组件。

父组件:

代码语言:txt
复制
import { Component, ViewChild } from '@angular/core';
import { MyService } from './my.service';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-parent',
  template: `
    <app-child></app-child>
  `,
  providers: [MyService]
})
export class ParentComponent {
  @ViewChild(ChildComponent) childComponent: ChildComponent;
  myService: MyService;

  constructor() {
    this.myService = new MyService();
  }

  ngAfterViewInit() {
    this.childComponent.myService = this.myService;
  }
}

子组件:

代码语言:txt
复制
import { Component } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-child',
  template: `
    <p>{{ myService.getData() }}</p>
  `
})
export class ChildComponent {
  myService: MyService;
}
  1. 使用共享服务:创建一个共享服务,将需要在父子组件之间共享的数据和方法放在该服务中,并在父组件和子组件中分别注入该共享服务。

共享服务:

代码语言:txt
复制
import { Injectable } from '@angular/core';

@Injectable()
export class SharedService {
  data: any;

  setData(data: any) {
    this.data = data;
  }

  getData() {
    return this.data;
  }
}

父组件:

代码语言:txt
复制
import { Component } from '@angular/core';
import { SharedService } from './shared.service';

@Component({
  selector: 'app-parent',
  template: `
    <button (click)="setData()">Set Data</button>
    <app-child></app-child>
  `,
  providers: [SharedService]
})
export class ParentComponent {
  constructor(private sharedService: SharedService) {}

  setData() {
    this.sharedService.setData('Hello from parent');
  }
}

子组件:

代码语言:txt
复制
import { Component } from '@angular/core';
import { SharedService } from './shared.service';

@Component({
  selector: 'app-child',
  template: `
    <p>{{ sharedService.getData() }}</p>
  `
})
export class ChildComponent {
  constructor(public sharedService: SharedService) {}
}

这些方法可以根据具体的需求选择使用,以实现在父组件中注入的服务能够在子组件中使用。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

16分8秒

Tspider分库分表的部署 - MySQL

1时8分

TDSQL安装部署实战

扫码

添加站长 进交流群

领取专属 10元无门槛券

手把手带您无忧上云

扫码加入开发者社群

相关资讯

热门标签

活动推荐

    运营活动

    活动名称
    广告关闭
    领券