In spectator mode, you cannot interact with the items in the inventory in any way. But with the help of the /enchant command, you can enchant items in this mode. Reproduce:
Take any item from the creative inventory that can be enchanted by this command.
Switch to the spectator mode - F3+F4, F3+N, /gamemode spectator.
Use the command /enchant @s minecraft:loyayty 3 (I tested this on a trident, but it works on any item and any enchantment can also be cast)
An error message will not appear, something like «It is impossible to enchant items in the spectator mode», and the loyalty spell will be successfully applied to the trident and they will fully work - after the throw, the weapon will turn towards you.
Thank you for helping us improve Minecraft! We saved your files: