简体   繁体   English

将子组件注入父组件会扭曲样式

[英]injecting child component into parent distorts the style

I have asked this before and others did but for the life of me, I still can't fix it. 我之前曾问过这个,其他人也曾问过,但对于我的一生,我仍然无法解决。 Here is a stackblitz for you to see: https://stackblitz.com/edit/angular-dpcbxm 这是一个让您看到的stackblitz: https ://stackblitz.com/edit/angular-dpcbxm

which is pretty descriptive. 这非常具有描述性。 I honestly have read a lot about view encapsulation and how it affects injected child components but the 3 settings didn't change anything (nor I want to mess with it in the first place). 老实说,我已经阅读了很多有关视图封装及其如何影响注入的子组件的信息,但是这3个设置没有任何改变(我也不希望一开始就弄乱它)。 I tried doing it via selector: '[hello]' then ..... as well but it changes nothing. 我尝试通过选择器:'[hello]'然后也执行...,但是它什么也没有改变。

I notice a lot of styles are inserted such as ng_content-c4, c6... while I don't know why the child component would have different values than others, I do get they are internal. 我注意到插入了许多样式,例如ng_content-c4,c6 ...虽然我不知道为什么子组件的值会与其他组件不同,但我知道它们是内部的。

In the angular encapsulation page, I did see about styling such components [_ng_content-c6] etc but they are applied to different elements of the child component at a given time that I don't know what to style nor what style to define for them since the style I need is defined for them. 在角度封装页面中,我确实了解了有关对此类组件[_ng_content-c6]等进行样式设置的信息,但是在给定的时间将它们应用于子组件的不同元素,我不知道该如何样式化,也不知道为其定义什么样式因为我需要为他们定义样式。

Can someone take a look and help me fix it? 有人可以看一下并帮助我修复它吗?

I was able to apply the styles defined on the parent to the child component by disabling style encapsulation. 通过禁用样式封装,我能够将在父项上定义的样式应用于子组件。

On Parent: 在父母上:

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

and also 并且

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  encapsulation: ViewEncapsulation.None
})

I'm getting some erros on the Blitz you share, but at least styling is working. 我在分享的闪电战中遇到了一些麻烦,但至少可以使用样式。 https://stackblitz.com/edit/angular-4grbtd https://stackblitz.com/edit/angular-4grbtd

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

相关问题 将不同的父组件注入子组件 - Injecting different parent components into child component 应用于Angular 2中的父组件的子样式? - Child style applying to parent component in Angular 2? 为什么在子组件中将封装设置为none会影响父组件样式? - Why setting encapsulation to none in a child component affects parent component style? Angular 从父组件更改子组件样式但不是全局更改 - Angular change child component style from parent component but not globally 如何从父组件设置递归嵌套子组件的样式 - How to style recursively nested child component from parent component 当我们有 2 个子组件时,如何将样式从父组件应用到子组件 - How to apply style to child component from parent component when we have 2 child component 防止子组件<style> added with innerHtml to be applied to parent - Prevent child component <style> added with innerHtml to be applied to parent 当子组件在不同的父组件中时,它的样式不同 - Style child component differently when it is in different parent components 防止子组件从父组件继承样式 - Prevent Child component from taking style from Parent 从父组件到子组件Angular2 / 4触发悬停样式 - Trigger hover style from parent to child component Angular2/4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM