How to properly program AJAX links? I have a custom module that uses AJAX. Everything works fine the way it is. I'm just wondering if it's the proper way to do it in Drupal. I'm new to Drupal 8 and 9 programming
This is a line from my code
$link ='&lt;a href=&quot;/nojs/my-link/1&quot; class=&quot;use-ajax btn btn-default&quot;&gt;Test AJAX&lt;/a&gt;';

Is this ok to do or should I be building my link? If so, why and how?
EDIT
I currently build non-ajax links like
  $url = Url::fromRoute('entity.node.canonical', array('node' =&gt; $record-&gt;nid));
  $title_link = Link::fromTextAndUrl($record-&gt;title, $url);
  $title_link =$title_link-&gt;toRenderable();
  $title_link=render($title_link);
If you just need to add the classes I believe it can be done like so
$url = Url::fromRoute(
  'entity.node.canonical', 
  ['node' =&gt; $record-&gt;nid], 
  ['#attributes' =&gt; ['class' =&gt; ['btn', 'btn-default', 'use-ajax']]]
);

 $title_link = Link::fromTextAndUrl($record-&gt;title, $url);

At that point $title_link should be able to be rendered in the template as is!
// php
$variables['title_link'] = $title_link;

// twig
{{ title_link }}