简体   繁体   English

为什么 CSS 样式不适用于带有 Vaadin 元素的 Angular 2 组件?

[英]Why does CSS Styles not apply on Angular 2 component with Vaadin elements?

I am busy doing the tutorial on Vaadin elements using Angular 2 which I found here我正忙着使用 Angular 2 完成有关 Vaadin 元素的教程,我在这里找到了

https://vaadin.com/docs/-/part/elements/angular2-polymer/tutorial-index.html https://vaadin.com/docs/-/part/elements/angular2-polymer/tutorial-index.html

In chapter 5.3, Styles are applied to the app.component.ts as follow在第 5.3 章中,样式应用于 app.component.ts 如下

import { Component } from [email protected]/core';

@Component({
  selector: 'my-app',
  template: `
    <app-header-layout has-scrolling-region>
      <app-header fixed>
        <app-toolbar>
          <div title spacer>All heroes</div>
        </app-toolbar>
      </app-header>
      <div>My application content</div>
    </app-header-layout>
  `,
  styles: [`
    app-toolbar {
      background: var(--primary-color);
      color: var(--dark-theme-text-color);
    }
  `]
})
export class AppComponent { }

The variables in styles does not get applied in the component but when I change the color to red or blue like so:样式中的变量不会应用到组件中,但是当我将颜色更改为红色或蓝色时,如下所示:

app-toolbar {
      background: red;
      color: blue;
    }

It actually works, meaning that the variables are not found.它实际上有效,这意味着找不到变量。

I went to the index.html and saw that there is a style being applied to the body tag which also has variables.我去了 index.html 并看到有一个样式应用于 body 标签,它也有变量。

  <!-- Styles -->
  <link rel="import" href="bower_components/iron-flex-layout/iron-flex-layout.html">
  <link rel="import" href="bower_components/paper-styles/color.html">
  <link rel="import" href="bower_components/paper-styles/default-theme.html">
  <link rel="import" href="bower_components/paper-styles/typography.html">
  <link rel="import" href="bower_components/paper-styles/shadow.html">
  <style is="custom-style">
    body {
      @apply(--layout-fullbleed);
      @apply(--paper-font-body1);
    background: var(--primary-background-color);
      color: var(--primary-text-color);
    }
  </style>

Though these variables are actually found and applied.虽然这些变量实际上是被发现和应用的。 I tested it out by swapping out for another variable as seen below:我通过换出另一个变量来测试它,如下所示:

 <style is="custom-style">
        body {
          @apply(--layout-fullbleed);
          @apply(--paper-font-body1);
          background: var(--paper-pink-a200);
          color: var(--primary-text-color);
        }
      </style>

This variable turns the background pink and actually works.这个变量将背景变成粉红色并且实际起作用。

These variables are mainly found in the color.html and default-theme.html and gets initialized as follow这些变量主要存在于 color.html 和 default-theme.html 中,初始化如下

default-theme.html默认主题.html

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="color.html">

<style is="custom-style">

  :root{
    --primary-text-color: var(--dark-theme-text-color);
    --dark-theme-text-color: #ffffff;
    --primary-color: var(--paper-indigo-500);
</style>

color.html颜色.html

<link rel="import" href="../polymer/polymer.html">

<style is="custom-style">
    --paper-indigo-500: #3f51b5;
    --paper-pink-a200: #ff4081;
</style>

For some reason variables get applied in the index.html but not the app.component.ts and I followed the tutorial step by step.出于某种原因,变量被应用到 index.html 而不是 app.component.ts,我一步一步地跟着教程。 Can you help me solve this please ?你能帮我解决这个问题吗?

EDIT: Also added "emitDecoratorMetadata": true to my code as a comment (Which had since been removed for some reason) suggested and in my Crome console there are no errors nor any warnings about this issue.编辑:还添加了"emitDecoratorMetadata": true作为我的代码作为注释(由于某种原因已被删除),并且在我的 Crome 控制台中没有关于此问题的错误或任何警告。 If there is anything extra you need ask and I shall provide如果有什么额外的你需要问,我会提供

Angular doesn't support CSS variables and mixins. Angular 不支持 CSS 变量和 mixin。 That's a Polymer feature and only works in Polymer elements.这是一个 Polymer 功能,仅适用于 Polymer 元素。

As a workaround you could try to create a wrapper Polymer element where you put the styles and wrap the Vaadin element with that Polymer wrapper element.作为一种解决方法,您可以尝试创建一个包装 Polymer 元素,您可以在其中放置样式并使用该 Polymer 包装元素包装 Vaadin 元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM