Neither the property nor one of the methods exists and has public access in the class, but exists

62 Views Asked by At

I have a bit of a problem that I don't understand at all.

Neither the property "selected" nor one of the methods "selected()", "getselected()"/"isselected()"/"hasselected()" or "__call()" exist and have public access in class "App\Twig\Components\MenuItem".

Selected property is well and beautifully defined and in public. I don't know what the problem is

// 
<?php

namespace App\Twig\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
final class MenuItem
{
    public string $selected;
}
       :class="{'rotate-180': (selected ==='{{ this.selected }}')}"
      ></i>
    </a>
  {% else %}
    <a
      x-init="console.log('{{ this.selected }}')"
1

There are 1 best solutions below

0
Simon On

When creating TwigComponents you don't need to do {{ this.var }} to reference the variable created in the PHP class, you can just use {{ var }}.

Changing your code to {{ selected }} instead of {{ this.selected }} should resolve the issue.