使用不同组件中的服务无法正常工作

时间:2019-03-11 07:42:49

标签: angular angular-services subject-observer

我有一个侧面导航栏和一个内容div。我当前想要实现的是,每当我单击侧面导航中的任何元素时。该导航项的innerText应该显示在内容div中。我的代码如下

sidenav.component.ts

import {Component} from '@angular/core';
import {FlatTreeControl} from '@angular/cdk/tree';
import {MatTreeFlatDataSource, MatTreeFlattener} from '@angular/material';
import {BlogService} from '../../../../services/blog.service';

interface FoodNode {
  name: string;
  children?: FoodNode[];
}

const TREE_DATA: FoodNode[] = [
  {
    name: 'Fruit',
    children: [
      {name: 'Apple'},
      {name: 'Banana'},
      {name: 'Fruit loops'},
    ]
  }, {
    name: 'Vegetables',
    children: [
      {
        name: 'Green',
        children: [
          {name: 'Broccoli'},
          {name: 'Brussel sprouts'},
        ]
      }, {
        name: 'Orange',
        children: [
          {name: 'Pumpkins'},
          {name: 'Carrots'},
        ]
      },
    ]
  },
];

interface ExampleFlatNode {
  expandable: boolean;
  name: string;
  level: number;
}


@Component({
  selector: 'app-sidenav',
  templateUrl: './sidenav.component.html',
  styleUrls: ['./sidenav.component.css'],
  providers: [BlogService]
})
export class SidenavComponent {


  treeControl = new FlatTreeControl<ExampleFlatNode>(
    node => node.level, node => node.expandable);

  private transformer = (node: FoodNode, level: number) => {
    return {
      expandable: !!node.children && node.children.length > 0,
      name: node.name,
      level,
    };
  }

  treeFlattener = new MatTreeFlattener(
    this.transformer, node => node.level, node => node.expandable, node => node.children);

  dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);

  constructor(private blogService: BlogService) {
    this.dataSource.data = TREE_DATA;
  }

  hasChild = (_: number, node: ExampleFlatNode) => node.expandable;

  routeToPage(event: any) {

    this.blogService.changeSelectedNode(event.target.innerText);
  }
}

content.component.ts

import {Component, OnInit} from '@angular/core';
import {BlogService} from '../../../../services/blog.service';

@Component({
  selector: 'app-content',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.css'],
  providers: [BlogService]
})
export class ContentComponent implements OnInit {
  page: string;

  constructor(private blogService: BlogService) {
    this.page = this.blogService.selectedNodeName;
    console.log(this.page);
  }

  ngOnInit() {
  }
}

blog.service.ts

import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';

@Injectable()
export class BlogService {

  public selectedNodeName: string;
  public sideNavToggle: boolean;
  public selectedNode: Subject<string> = new Subject<string>();
  public sideNavDisabled: Subject<boolean> = new Subject<boolean>();

  constructor() {
    this.selectedNode.subscribe((node) => {
      this.selectedNodeName = node;
    });
  }

  changeSelectedNode(node: string) {
    this.selectedNode.next(
      this.selectedNodeName = node
    );
  }
}

请帮助我解决此问题。我可以成功地将内容传递给服务,但是每次单击侧面导航项时,内容ts文件中的代码都不会执行。

我的完整代码可以在这里找到-https://stackblitz.com/github/vibhorgoyal18/atest-blog

1 个答案:

答案 0 :(得分:0)

花了我一段时间,但这是您的示例已更新并可以运行:Demo

这就是我所做的更改:

首先,您的服务定义不正确。您已在SideNavComponent和ContentComponent中添加了[providers:BlogService]之类的服务。这些组件然后没有获得相同的BlogService实例,因此它不起作用。您必须将提供程序:[BlogService]放入AppModule。

第二:我删除了服务中您要订阅的部分,并将其移至ContentComponent。否则,Angular无法正确绑定到该变量。

编辑: 如果您使用的是Angular 6+,也可以在服务中添加它,并将其从模块的提供者中删除:

@Injectable({
    providedIn: 'root'
})

这将在您所有的应用程序中提供服务。如果只希望在一个模块中提供服务,则可以指定该模块为root。