typescript - 我的 service.ts 没有在我的组件中调用?
问题描述
在我的 Angular 项目中,我有一个 home 组件。在那个组件中,我有一个方法。所以我有一个 service.ts 并将该服务导入到我的 homecomponent.ts 文件中。我在 service.ts 文件中有一个类似 fliterclicked 和条件的方法。但是在运行时,当我单击该方法时出现此错误 - 'fliterclicked' 不是函数
主页组件.html:
<button (click)="filterClicked()"
[class.active]="filterChanged"
id="filterDropdown" type="button"
class="dropdown-toggle btn btn-clear-default job-filter"></button>
homecomponent.ts:
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { FilterService } from "../../services/filter.service";
@Component({
selector: 'jobs-sub-header',
templateUrl: './sub-header.component.html',
providers: [ FilterService ]
})
export class SubHeaderComponent implements OnInit {
constructor(
private filterService: FilterService) {
}
我的服务.ts:
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class FilterService {
filterClicked(){
this.filterShow = !this.filterShow;
}
}
我不知道我错过了哪里......请有人帮我解决这个问题吗?
解决方案
从 html 直接调用 filterClicked() 只会检查您的组件方法。filterService.filterService()
因此,您必须像在 Homecomponent.html 中一样提及它。在您的 ts 构造函数中公开您的服务。
主页组件.ts
constructor(
public filterService: FilterService) {
}
主页组件.html:
<button (click)="filterService.filterClicked()"
[class.active]="filterChanged"
id="filterDropdown" type="button"
class="dropdown-toggle btn btn-clear-default job-filter"></button>
推荐阅读
- angular - 自定义 AWS Amplify cognito 登录/注册组件?
- javascript - firebase 函数返回未定义的预期承诺或值
- c - 在 C 中的 bmp 图像之间导航/滚动
- perl - 如何使用 Net::SSH::Perl 检查远程目录是否存在
- c# - 使用 Task.run 和不使用 Task.run 将项目发布到 TPL 操作块
- elasticsearch - 无法在 Elastic Cloud 用户设置中设置 thread_pool 设置
- angular - 带有多选选项的 Angular Typeahead
- linux-kernel - xhci_urb_enqueue() 如何知道在 Linux USB 堆栈中将控制请求发送到哪个端点?
- mapbox - 使用 Jumpto 而不是 Flyto 的 Mapbox 地理编码?
- nginx - 如何禁用 NGINX 静态媒体 URL 根