How do I use a display mode for anonymous users, and another one for logged-in users? I would like to change the display mode only for entity.node.canonical, so that a logged-in user would see something completely different than anonymous users.  

For example, an entity displayed by an entity reference field should be displayed in teaser mode for anonymous users, and in full mode for logged-in users.

How can I achieve this?
This is similar to How do I change the node view mode before rendering?, only that you want to add conditions for the route name and the current user:

/**
 * Implements hook_entity_view_mode_alter().
 */
function mymodule_entity_view_mode_alter(&amp;$view_mode, $entity, $context) {
  if ($entity-&gt;getEntityTypeId() == 'node' &amp;&amp; $entity-&gt;bundle() == 'article') {
    if (\Drupal::routeMatch()-&gt;getRouteName() == 'entity.node.canonical') {
      if (\Drupal::currentUser()-&gt;isAnonymous()) {
        $view_mode = 'teaser';
      }
    }
  }
}