I am following the Angular.io guide for angular animations but have run into an error.
This is the sample code they provided.
export class Hero {
constructor(public name: string, public state = 'inactive') { }
toggleState() {
this.state = this.state === 'active' ? 'inactive' : 'active';
}
}
While testing my service, I was able to properly inject the service into my navbar component and my basic toggleState() function works. However, when I add the public state = 'inactive' text into my service constructor I get an error.
Arguments array must have arguments.
I have been reading up on services and DI and cannot find the correct articles which can explain to me how to use public constructors in a service so I can fix my error.
My current code:
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();
}
sidebar.service.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()">