简体   繁体   English

Angular,使用基于父组件的短组件选择器

[英]Angular, Use short component selector based on the parent component

I am developing a library for the UI components.我正在为 UI 组件开发一个库。 I created a parent component (using ng-content) that assigns css to the children, and declares that this part of the interface will be built using the library's element components.我创建了一个父组件(使用 ng-content),将 css 分配给子组件,并声明这部分接口将使用库的元素组件构建。

I would like to avoid having writing every time a full name with prefix for the child components.我想避免每次都为子组件编写带有前缀的全名。

For example I want to build a form component:例如我想构建一个表单组件:

<dvl-ui-theme theme="basic">
  <dvl-ui-active-form>
    <dvl-ui-input name="input-name">
  </dvl-ui-active-form>
</dvl-ui-theme>

I would like to use something like我想使用类似的东西

<dvl-ui-theme theme="basic">
  <active-form>
    <input name="input-name">
  </active-form>
</dvl-ui-theme>

It will be shorter and much more readable.它将更短且更具可读性。

Is there a way I can achieve this?有没有办法可以做到这一点?

EDIT: To summarize and make it clear - I want to have different selector of the component based on the parent component element.编辑:总结并说清楚 - 我想根据父组件元素有不同的组件选择器。

Normally I would use <dvl-ui-input> but since the input is in the form, I would like to use <input> and have the same component initialized通常我会使用<dvl-ui-input>但由于输入是表单,我想使用<input>并初始化相同的组件

The Selector of an angular Component is defined in the ***.component.ts file: angular 组件的选择器在***.component.ts文件中定义:

@Component({
  selector: 'app-root', // <= here you can change the selector of your component
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {}

this will look like so in your.html file:这在您的.html 文件中看起来像这样:

<app-root></app-root>

Be aware that duplicate selectors will cause issues.请注意,重复的选择器会导致问题。 Also you cannot use plain html tag names as your component selector like: input , div , h1 , etc...您也不能使用普通的 html 标签名称作为组件选择器,例如: inputdivh1等...

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

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