How to attach a CSS or JS library to a View in Drupal 8?

 In .module file hook_views_pre_render().
<?php
/**
* Implements hook_views_pre_render().
*/
function hook_views_pre_render(ViewExecutable $view) {
  if (isset($view) && ($view->storage->id() == 'blog')) {
    $view->element['#attached']['library'][] = 'custom/blog';
  }
}
?>

Tags