Angular 7“期望'样式'为字符串数组”

wub*_*dub 12 javascript string compiler-errors angular angular7

我正在尝试运行服务器,并在localhost:8000上加载app.component.html。相反,我收到此错误

editor.js:7992未捕获的错误:预期“样式”为字符串数组。位于> CompileMetadataResolver.push ../ node_modules/@angular/compiler/fesm5/compiler.j> s.CompileMetadataResolver.getNonNormalizedDirectiveMetadata >>(compiler.js:17325)处的assertArrayOfStrings(compiler.js:7992)处> CompileMetadataResolve ./node_modules/@angular/compiler/fesm5/compiler.j>s.CompileMetadataResolver._getEntryComponentMetadata(compiler.js:17970)在compiler.js:17630在Array.map()在> CompileMetadataResolver.push ../ node_modules / @ angular / compiler / fesm5 / compiler.j> s.CompileMetadataResolver.getNgModuleMetadata(compiler.js:17630),位于> JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompile> r._loadModules(com .js:24899),网址为> JitCompiler.push.。

我尝试弄乱语法并检查了angular.json文件。

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'public';
}
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个问题的任何想法?这阻止了我加载Google

mkh*_*ra2 12

angular.json的中引用的样式表styles[]不能在Component的styleUrls[]装饰器中引用。

确保./app.component.css两个文件都没有引用。


小智 7

在升级到Angular 7并将各种依赖项升级到其最新版本的过程中,我也得到了这一点。我发现即使我的styleUrls语法正确,使用原始加载程序2.0.0版也会导致此错误。降级到原始加载程序版本1.0.0解决了此问题。我不知道这是否对您有帮助。


Mac*_*ski 5

就我而言,发生这种情况是因为在多仓库项目中,在 Angular 构建过程中使用了错误的加载器 ( raw-loader@2),所以我必须将正确的加载器添加到 Angular 项目依赖项中:

devDependencies: {
    ...
    raw-loader: "^1.0.0"
}
Run Code Online (Sandbox Code Playgroud)


eit*_*ama 5

我有同样的控制台错误,花了几天时间阅读谷歌上的每一次点击......

解决了!

故事:我必须为现有的 Angular 项目添加功能。当我执行 git clone, ng serve, open browser -> error assert....

我决定升级到 Angular 8,看看问题是否会消失,但并没有。最终,我使用“ng new ...”创建了一个新的 Angular 8 应用程序

比较现有项目和新项目的 package.json 和 angular.json。我发现,全新的 Angular 应用程序在 package.json 中没有加载程序包,而现有的却有。此外,现有项目指定了 webpack devDependancy,我删除了所有加载程序和 webpack,删除了 package-lock.json 和 node_modules 文件夹 -> npm install。

解决了。我猜有一些代码被迫使用特定版本,与 Angular 真正需要的东西相冲突。