te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>css - How to change Angular Material v19 component colors based on routing, and manage all colors in a single SCSS file? - Stack
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - How to change Angular Material v19 component colors based on routing, and manage all colors in a single SCSS file? - Stack

programmeradmin3浏览0评论

I am using Angular Material v19 (MDC) along with custom Angular components (e.g., headerponent.ts) in my project. Inside these components, I often use Material components like mat-button, but with custom classes (e.g., .manage-button).

I need to apply different styles (e.g., background color, text color) to my custom components and buttons depending on the current route. For example:

  • On /dashboard, the background color of the headerponent should be blue, and the .manage-button inside it should be light-blue.
  • On /company, the same headerponent should have a green background, and .manage-button should be white.

My Requirements:

  • Styles should change based on routing.
  • All route-specific colors should be defined in a single SCSS file for easy maintenance.
  • Some styles affect custom components like headerponent, and some styles apply to Material components like mat-button with custom classes (e.g., .manage-button).

My questions:

  • What is the best practice to handle route-based styling for custom components + Material components?
  • How should I combine Material theming APIs like mat.button-theme() with my own custom component styles in such a setup?

I am using Angular Material v19 (MDC) along with custom Angular components (e.g., headerponent.ts) in my project. Inside these components, I often use Material components like mat-button, but with custom classes (e.g., .manage-button).

I need to apply different styles (e.g., background color, text color) to my custom components and buttons depending on the current route. For example:

  • On /dashboard, the background color of the headerponent should be blue, and the .manage-button inside it should be light-blue.
  • On /company, the same headerponent should have a green background, and .manage-button should be white.

My Requirements:

  • Styles should change based on routing.
  • All route-specific colors should be defined in a single SCSS file for easy maintenance.
  • Some styles affect custom components like headerponent, and some styles apply to Material components like mat-button with custom classes (e.g., .manage-button).

My questions:

  • What is the best practice to handle route-based styling for custom components + Material components?
  • How should I combine Material theming APIs like mat.button-theme() with my own custom component styles in such a setup?
Share Improve this question asked Feb 17 at 12:29 Federica TomolaFederica Tomola 634 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

In the component that contains the <router-outlet />

@Component({
  imports: [RouterOutlet],
  template: `
    <router-outlet />
  `,
  host: {
    '[class]': 'clazz()',
  },
})
export class App {
  #router = inject(Router);

  currentRoute = toSignal(
    this.#router.events.pipe(
      filter((event) => event instanceof NavigationEnd),
      map(() => this.router.url)
    ),
    { initialValue: '' }
  );

  // probably missing some nuance
  // or use `endsWith()` or `includes()`
  clazz = computed(() => {
    const route = this.currentRoute();

    if (route.startsWith('/company')) {
      return 'company';
    } else if (route.startsWith('/dashboard')) {
      return 'dashboard';
    } else {
      return '';
    }
  });
}

That will apply the respective class to the whole component tree with those routes.

And then as far as Material styling goes in the desired SCSS file (the component with the router, or a "root" style file declared in the architect > build > options> styles array of the angular.json), use the "Styling" tab of a given component to find respective overrides.

@use '@angular/material' as mat;

pany {
  @include mat.button-overrides(
    (
      filled-container-color: blue,
      filled-label-text-color: white,
    )
  );
}
.dashboard {
  @include mat.button-overrides(
    (
      filled-container-color: white,
      filled-label-text-color: black,
    )
  );
}

As for stuff like the header component, you could scope the header's selector inside of one of those classes above, or in the header component check the current route and apply styles conditionally in the component.

First head to the styling tag of angular material components:

Material Button Styling

Get the customizations ready and use the below code, to add the styling based on the component, below is a sample of how it will look:


Global styles method:

If you are not a fan of ::ng-deep (Since it's usage is frowned upon by the community) you can add a class to the root route, this will be used to style the children

HOME:

<div class="class-to-customize-children">
  <!--  child HTML and components rendered directly or through routing -->
</div>

Then in the global styles add the following:

@use '@angular/material' as mat;

.class-to-customize-children {
  @include mat.button-overrides((
    filled-container-color: orange,
    filled-label-text-color: red,
  ));
}

ng-deep method:

@use '@angular/material' as mat;

:host ::ng-deep {
  @include mat.button-overrides((
    filled-container-color: orange,
    filled-label-text-color: red,
  ));
}

We use :host -> so that it applies styling only to the current component and it's children.

We use ::ng-deep -> so that it applies styling to angular material elements, which bypasses view encapsulation.


This is a component based approach, if you want a routing based approach.

When you visit and route and it sub routes, add the styling using the above method to the root component of the route, where the customizations will vary.

For example styling home/contact sub component add the styling to the route route homeponent.scss, these customizations will get applied to both home and home's children (contact component).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论