简体   繁体   中英

Aurelia component slotting in markup for components model

I am building an autocomplete component. The plan is that I can slot in some markup for what I know the component is going to bind to.

The idea is this could be any object rather than a simple display value and identifier.

I have this working using templates but I am wondering if there is a better approach.

So far it looks like this ( options is hard coded for now within the components model):

// Usage:
<autocomplete>
  <template replace-part="item">
    //this is the content for each option within the component
    <b>${option.lastName}<b/>, ${option.firstName}  
  </template>
</autocomplete>

//autocomplete 
<template>
  <input type="text" placeholder="Type 3 characters ...">
  <ul>
    <li repeat.for="option of options">
      <template replaceable part="item"></template>
    </li>
  </ul>
</template>

I don't really like the templating boilerplate, slots are much nicer, is there any way to make slots work like this?

<autocomplete>
    <li repeat.for="option of options">
        ${option.lastName}<b/>, ${option.firstName}
    <li/>
</autocomplete>

//autocomplete 
<template>
  <input type="text" placeholder="Type 3 characters ...">
  <ul>
    <slot></slot>
  </ul>
</template>

Slot in Aurelia is the emulation based on standard spec, which mean it doesn't work with repeat situation. repaceable was introduced to handle this scenario and I don't think we have any other options. Sometimes it feels weird but with a little documentation, probably you and your team will be fine. What you can do is for each replacement, what properties it can look for to get the item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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