angular - 角度动画和服务 - 错误:参数数组必须有参数
问题描述
我正在关注Angular.io角度动画指南,但遇到了错误。
这是他们提供的示例代码。
export class Hero {
constructor(public name: string, public state = 'inactive') { }
toggleState() {
this.state = this.state === 'active' ? 'inactive' : 'active';
}
}
在测试我的服务时,我能够将服务正确地注入到我的导航栏组件中,并且我的基本 toggleState() 函数可以正常工作。但是,当我将public state = 'inactive'文本添加到我的服务构造函数中时,我得到一个错误。
参数数组必须有参数。
我一直在阅读服务和 DI,但找不到正确的文章,可以向我解释如何在服务中使用公共构造函数,以便修复我的错误。
我当前的代码:
navbar.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';
import { SidebarService } from '../../services/sidebar.service';
@Component({
selector: 'app-nav-bar',
templateUrl: './nav-bar.component.html',
styleUrls: ['./nav-bar.component.css'],
})
export class NavBarComponent implements OnInit {
@Input() navbarState;
constructor(private sidebar: SidebarService) { }
ngOnInit() {
}
toggleNavbar() {
this.sidebar.toggleNavbar();
}
侧边栏服务.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class SidebarService {
constructor(public state = 'inactive') { }
toggleNavbar() {
if (this.state === 'inactive') {
console.log('it works');
}
}
}
navbar.component.html
<button class="navbar-toggler" type="button" (click)="toggleNavbar()">
解决方案
推荐阅读
- c++ - 如何计算while循环中的总平均值
- algolia - algolia:algoliasearch 没有调用签名
- apache-spark - Spark 执行器配置优先级
- android - 是否所有可在线绘制的 Re-sizer 无需确认即可免费使用
- fullcalendar - FullCalendar 更改日期格式
- reactjs - 导航到新页面不会导致前一页面被卸载
- python - 漂亮的打印/同时显示 2 个数据帧
- python - 将 matplotlib 图例另存为单独的图像
- amazon-web-services - Windows 10 为 openssh 导入 pem 文件
- python - sqlalchemy 引擎尝试连接到 127.0.0.1 而不是提供的 IP