Angular-scss (@include 与 @mixin) 样式封装! [同一标签不同样式]

8 篇文章 0 订阅
1 篇文章 0 订阅

 

① 创建一个 ui-theme 的组件。

  ui-theme.component.html

<div class="testDemo" [ngClass] = "{'red-theme': isPlus, 'blue-theme': !isPlus}">
        <div *ngIf = "isPlus" class="background">
            <p  class="font">plus 版</p>
        </div>
        <div *ngIf = "!isPlus" class="background">
            <p  class="font">common 版</p>
        </div>
</div>

  ui-theme.component.ts

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-ui-theme',
  templateUrl: './ui-theme.component.html',
  styleUrls: ['./ui-theme.component.scss']
})
export class UIThemeComponent implements OnInit {

  @Input() isPlus = false;
  constructor() { }

  ngOnInit(): void {
    this.isPlus = false;
  }

}

  ui-theme.component.scss

@import "../ui-theme-styles.scss";

@mixin theme($name, $background_color, $font_color, $font_size, $font_weight) {

    .testDemo.#{$name}{
       .background {
           height: 200px;
           width: 200px;
           line-height: 200px;
           background:$background_color;
           .font {
               color: $font_color;
               font-size: $font_size;
               text-align: center;
               font-weight: $font_weight;
           }
       }
    }
}

@include theme(red-theme, $background_color_red, $font_color_light, $font_size_18, $font_weight_600);
@include theme(blue-theme, $background_color_blue, $font_color_White, $font_size_24, $font_weight_300);

 ②创建一个公共的样式表。

  ui-theme-styles.scss

$background_color_red: #ff5b4d;
$background_color_blue: #34495e;
$font_color_White: #ffffff;
$font_color_light: #f8d5d5;
$font_size_18:18px;
$font_size_24:24px;
$font_weight_300: 300;
$font_weight_600: 600;

  ③在起始页 app.component.html 中应用 ui-theme 的组件 。

<app-ui-theme></app-ui-theme>

  完成! 随便写的一个小例子,喜欢的点赞!不喜勿喷,Thank you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值