【发布时间】: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