【问题标题】:Ionic and Angular: "::ng-deep" works with CSS, but not with preprocessors (SASS/SCSS)Ionic 和 Angular:“::ng-deep”适用于 CSS,但不适用于预处理器 (SASS/SCSS)
【发布时间】:2018-09-15 03:30:09
【问题描述】:

我遇到了一个有点奇怪的问题:::ng-deep 与常规 CSS 配合得很好,但是当我尝试切换到 SASS 时,我发现所有使用 ::ng-deep 声明的样式(我需要使用 @ 987654325@ 更改某些 Angular Material 样式),不适用。但所有其他样式都可以正常工作。

需要说,我使用的不是纯 Angular:

"ionic-angular": "3.9.2",
"@angular/*": "5.0.3",
"@ionic-native/*": "4.5.3",
...

这是我在 .angular-cli.json 中的defaults

{
  ...
  "defaults": {
    "styleExt": "scss", // also tried with "sass"
    "component": {}
  }
}

这是我的 sass.config.js(不确定您需要哪些行,所以我会发布完整文件):

module.exports = {
  outputFilename: process.env.IONIC_OUTPUT_CSS_FILE_NAME,
  sourceMap: false,
  outputStyle: 'expanded',
  autoprefixer: {
    browsers: [
      'last 2 versions',
      'iOS >= 8',
      'Android >= 4.4',
      'Explorer >= 11',
      'ExplorerMobile >= 11'
    ],
    cascade: false
  },
  includePaths: [
    'node_modules/ionic-angular/themes',
    'node_modules/ionicons/dist/scss',
    'node_modules/ionic-angular/fonts',
    'node_modules/font-awesome/scss'
  ],
  includeFiles: [
    /\.(s(c|a)ss)$/i
  ],
  excludeFiles: [
    /*  /\.(wp).(scss)$/i  */
  ],
  variableSassFiles: [
    '{{SRC}}/theme/variables.scss'
  ],
  directoryMaps: {
    '{{TMP}}': '{{SRC}}'
  },
  excludeModules: [
    '@angular',
    'commonjs-proxy',
    'core-js',
    'ionic-native',
    'rxjs',
    'zone.js'
  ]
};

我在迁移过程中是否遗漏了什么?如果您需要更多信息 - 只要问,我会更新问题。

component.ts:

@Component({
  selector: 'app-pincode-enter',
  templateUrl: './pincode-enter.component.html',
  styleUrls: ['/pincode-enter.component.scss'], // this path is correct, styles without "::ng-deep" works fine
  encapsulation: ViewEncapsulation.None // tried all 3 properties
})

pincode-enter.component.scss 在同一个文件夹中。 pincode-enter.component.scss:

/* Tried both with "::ng-deep" and without */
:host .mat-button-wrapper {
  line-height: 72px;
  background-color: red !important;
  color: yellow !important;
}

:host ::ng-deep .mat-button-wrapper {
  line-height: 72px;
  background-color: red !important;
  color: yellow !important;
}

以上样式不适用:

我也尝试将此样式移动到全局样式表,但再次失败。

【问题讨论】:

  • 编译时是否显示错误或静默失败?
  • 没有错误,:host ::ng-deep ... 的所有样式(我现在在 scss 上)都不适用(以及其他变体)。
  • 发布您的component.tsstyle.scss,只有相关部分才可以。我曾经将css 更改为sass 但忘记在component.ts 文件中更新:)
  • @sabithpocker,我已经更新了问题

标签: angular ionic-framework sass ionic3 angular-material


【解决方案1】:

你是否正确地逃避了它们?

style.sass

\:host /deep/ .changed
    color: red

另外,/deep/ is deprecated,所以最好不要使用它。

另一种方法是在您的组件中尝试switching off view-encapsulation,并使用\:host 限定样式,这样样式就不会泄漏

component.ts

encapsulation: ViewEncapsulation.None

还要检查这些recommendations from Material Team

这个Blog post也很实用,建议使用:ng-deep :)

我们了解组件范围的合法用例 CSS,您的样式会渗透到子组件中。这里有三个 实现这一目标的方法:

  1. 使用自定义属性(又名 CSS 变量) — 虽然浏览器还没有完全支持自定义属性,但浏览器 这些功能的实现将为开发人员提供最好的 隔离和灵活性的结合。自定义属性,在 效果,允许组件作者为 其组件的样式;他们让开发人员决定应该是什么 外部样式以及需要保持一致的内容。
  2. 使用全局范围的样式表和模拟封装 — 使用传统 CSS,您可以通过以下方式引用组件 名称作为 CSS 选择器的一部分,这将导致应用样式 像往常一样,包括深入到子组件, 假设您使用我们的模拟(默认)视图封装 你的组件。

  3. 使用 ::ng-deep — 如果您今天需要这个,请使用 ::ng-deep。这不应与任何 3rd 方工具或新浏览器冲突 发展。我们致力于保持 ::ng-deep 直到 基于标准的方法已获得全行业的支持。

【讨论】:

  • 我认为问题出在 Ionic 和 Angular 的集成中,因为它在纯 Angular 项目中有效。
  • 是的,在问这个问题之前,我已经尝试过封装这个技巧,但也没有运气。我知道它必须工作,但由于某种原因它不是。
  • 您能否发布一个与您的 DOM+Shadow-DOM 结构不兼容的示例 CSS
  • 好的,请稍等
  • 这真的很难理解发生了什么,但我非常感谢你的帮助。不管怎样,点个赞吧。
【解决方案2】:

由于 ::ng-deep/deep/>>> 将被弃用(或已经被弃用),你有吗?尝试 :host 代替?

【讨论】:

  • /deep/>>> 已弃用,是的,但不确定 ::ng-deep 是否也已弃用。我会在几分钟内尝试使用:host
  • /deep/ 和 >>> 是 ::ng-deep 的别名,所以如果是,它也是
猜你喜欢
  • 2020-07-20
  • 2017-08-02
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 2018-02-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多