【问题标题】:Unable to find CSS in angular无法在角度中找到 CSS
【发布时间】:2019-08-29 05:19:15
【问题描述】:

我已经使用 cli 生成了组件

"ng generate component registration"

它创建了以下文件

registration.component.html
registration.component.scss
registration.component.spec.ts
registration.component.ts

现在,我在 scss 文件中添加了一个 css 类

.loginBox {
 width: 260px;
}

现在在 html 文件中

<p class="loginBox"> test </p>

它指出 loginBox 在我的 IDE 中是未定义的 CSS,当我在 ng serve 中进行测试时,它没有显示应用了 css。

当我在 class= 上进行自动完成时,我看到有很多我没有添加的 css,例如 L0, L1, L2, L3 ....那么这些 css 来自哪里,为什么我的本地 css 不起作用?编辑:那些 css 看起来像是 bootstrap css 或有些内置 css 有类似 mat-action-row 或其他类型的 css 我不认为我包括它们。

我已经包含的ts文件styleUrls: ['./registration.component.scss']

编辑:即使我添加了全局styles.scss,它仍然说 loginBox 是未定义的 CSS。可能是某些配置或某些东西丢失了......我无法弄清楚它是什么......

编辑: registration.component.ts

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

@Component({
  selector: 'app-registration',
  templateUrl: './registration.component.html',
  styleUrls: ['./registration.component.scss']
})
export class RegistrationComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

registration.component.scss

.loginBox {
  /* color: #4C4633; */
  /* font-size: 12px; */
  /* margin-bottom: 10px; */
  /* padding: 10px 0px 10px 10px; */
  width: 260px;
}

registration.component.html

<p class="loginBox">
  registration works!
</p>
<div class="loginBox"> test </div>

注册文件夹上方..

app.component.html

<div class="container-fluid p-0">
  <router-outlet></router-outlet>
</div>

app.module.ts

const routes: Routes = [
  { path: '', component: RegistrationComponent}
];
@NgModule({
  declarations: [RegistrationComponent],
imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  providers: [PreviousUrlService, {provide: APP_BASE_HREF, useValue : '/' }],
  bootstrap: [AppComponent]
})
export class AppModule { }

【问题讨论】:

  • 你构建了项目的 css 了吗? scss 文件通常不会在生产中加载;它们需要被构建到css 文件中。
  • 构建你的意思是'ng build'?在我进行 ng 构建后它不起作用
  • 我们能看到实际的错误吗?
  • 没有错误。这正是 IDE 所指出的。 '在这一行有多个标记 - 未定义的 CSS 类 (loginBox)。 - 1 条更改线'
  • 能否分享一下组件的ts文件,看看有没有小错误?

标签: javascript html css angular


【解决方案1】:

看到这个answer here。在@component 定义中使用encapsulation: ViewEncapsulation.None

【讨论】:

    【解决方案2】:

    scss 是否正确读取?尝试运行此命令

    ng config schematics.@schematics/angular:component.styleext scss

    然后,重新运行你的项目

    【讨论】:

    • 我的 Angular.json 中有这个 ` "schematics": { "@schematics/angular:component": { "styleext": "scss" } }`
    【解决方案3】:

    尝试:

    :host ::ng-deep{
      .loginBox {
        width: 260px;
      }
    }
    

    【讨论】:

    • 您能否为您的答案添加更多上下文?这应该解决什么问题和/或您为什么推荐它?
    猜你喜欢
    • 2017-06-15
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多