首页 > 解决方案 > 角度动画和服务 - 错误:参数数组必须有参数

问题描述

我正在关注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()">

标签: angulardependency-injectionangular-animations

解决方案


推荐阅读