Expected 'styles' to be an array of strings. Angular2

心已入冬 提交于 2021-02-19 05:48:25

问题


I get such error

Uncaught Error: Expected 'styles' to be an array of strings.

Here is my code

styleUrls:[
    "../../styles/login.component.styles.scss"
],

When I have them declared in the same way but, the file with styles exist in the same directory as the component the problem does not occur.


回答1:


I filed an issue for this, which was triaged as workaround 2: non-obvious, priority: 2 (required), severity 3: broken.

I was struggling with something similar while porting an existing angular 2 beta-6 application to angular-cli 1.2.6; we use SCSS throughout.

Caveat: I do not know if the modifications I was required to make (outlined below) are artifacts from our legacy architecture, are limitations of the framework, or are something else, but they worked for me.

After much debugging and trying recommendations from various sources, I discovered that some webpack magic causes the compiled CSS resulting from a file reference in a @Component's styleUrl[] which is also in .angular-cli.json's styles[] array to be interpreted as {}. The expected output is a string representation of the compiled SCSS file as CSS.

This will then cause angular-compiler's assertArrayOfStrings() call to throw with none other than Expected '" + identifier + "' to be an array of strings.

I had to make the following changes to my application to get it to work:

  1. Global stylesheet references that were app.component's styleUrls[] array must be moved to .angular-cli.json's app[0].styles[] array.
  2. app.component's styleUrls[] array should be empty.
  3. app.component's encapsulation: ViewEncapsulation.None meta-decorator can be removed.
  4. Stylesheets which are referenced in .angular-cli.json's app[0].styles[] array must not be referenced in any Component's styleUrls[] decorator.


来源:https://stackoverflow.com/questions/44241725/expected-styles-to-be-an-array-of-strings-angular2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!