[英]Angular2 component without selector tag in DOM
Is there a way, with Angular2, to call a component by a selector without this selector appears in the DOM ?有没有办法使用 Angular2 通过选择器调用组件而不出现在 DOM 中?
My problem is I want to make a game in Angular2 with SVG rendering.我的问题是我想在 Angular2 中制作一个带有 SVG 渲染的游戏。 I have a match component (my main match container), who calls a field component (who draws the field).
我有一个匹配组件(我的主匹配容器),它调用一个字段组件(谁绘制该字段)。 My match component is a SVG element, and the field is a SVG Rect element.
我的匹配组件是一个 SVG 元素,该字段是一个 SVG Rect 元素。
This is my match component :这是我的匹配组件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'match',
template: `
<svg:svg width="800" height="600" style="background-color:purple;">
<field></field>
</svg:svg>
`
})
export class MatchComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
And my field component :而我的字段组件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'field',
template: `
<svg:rect width="600" height="300" fill="yellow"></svg:rect>
`
})
export class TerrainComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
When I launch my solution, it's working but the SVG is not like I want causes there is a element in the DOM who breaks the SVG :当我启动我的解决方案时,它正在工作,但 SVG 不是我想要的,因为 DOM 中有一个元素破坏了 SVG:
<match>
<svg height="600" style="background-color:purple;" width="800">
<field>
<rect fill="yellow" height="300" width="600"></rect>
</field>
</svg>
</match>
Do you know if it's possible to use selectors whitout have them in the result DOM ?您知道是否可以在结果 DOM 中使用选择器而不将它们包含在结果 DOM 中? Or is there a better way to generate SVG with Angular 2 ?
或者有没有更好的方法来使用 Angular 2 生成 SVG?
EDIT : I found a solution using SVG groups, but I don't think it's the best solution, I would prefer not have the selector tag in the DOM at all.编辑:我找到了一个使用 SVG 组的解决方案,但我认为这不是最好的解决方案,我宁愿在 DOM 中根本没有选择器标签。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'match',
template: `
<svg:svg width="800" height="600" style="background-color:purple;">
<g field></g>
</svg:svg>
`
})
export class MatchComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'g[field]',
template: `
<svg:rect width="600" height="300" fill="yellow"></svg:rect>
`
})
export class TerrainComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
It's rendering this SVG, who works :它正在渲染这个 SVG,谁工作:
<match>
<svg height="600" style="background-color:purple;" width="800">
<g field="">
<rect fill="yellow" height="300" width="600"></rect>
</g>
</svg>
</match>
You can solve this by using CSS only, just set match
as display: contents
,您可以仅使用 CSS 来解决此问题,只需将
match
设置为display: contents
,
match {
display: contents;
}
As stated on display: contents
documentation , this causes to appear as if the component were direct children of the element's parent.如
display: contents
documentation 所述,这会导致组件看起来好像是元素父元素的直接子元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.