首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义组件的问题

问自定义组件的问题
EN

Stack Overflow用户
提问于 2019-05-02 02:35:37
回答 1查看 46关注 0票数 0

我正在学习lit-element,lit-html。我尝试创建的组件是一个自定义选择组件。我在填充下拉值和选项时遇到问题。我正在寻求关于我下一步应该做什么的指导,看看我是否在正确的轨道上。

我曾尝试使用repeat函数来迭代选项数组,但它不起作用。

这是我的select组件的代码。

代码语言:javascript
复制
import {LitElement, html} from 'lit-element';
import {repeat} from 'lit-html/lib/repeat.js';

class HiSelect extends LitElement{
    static get properties(){
        return{
            SelName: String,
            SelplaceHolder: String,
            SellabelName: String,
            SelValue: Array
        }
    }

    constructor(){
        super();
    }

    render(){
        return html`
        <div class="form-field">
            <div class="form-field__control">
                <select id="form-field-select" class="form-field__select" 
                 name="${this.SelName}">
                    ${repeat(this.SelValue, (option) => html`<option>${option.value} 
                </option>`)};
                </select>
                <label for="${this.SelName}" class="form- 
                  field__label">${this.SellabelName}</label>
                <div class="form-field__bar"></div>
            </div>
        </div>
        `
    }
}
customElements.define('hi-select',HiSelect);

这是我的app.js的代码

代码语言:javascript
复制
import {LitElement, html} from 'lit-element';
import './Hi-TextBox.js';
import './Hi-TextArea.js';
import './Hi-Select.js';

class Components extends LitElement{
    static get properties(){
        return{

        }
    }

    constructor(){
        super();
        this.labelName="Category";
        this.name="Category";
        this.value="";
        this.placeHolder=" ";
        this.Columns = 30;
        this.Rows = 10;
        this.TaName = "Description";
        this.Taplaceholder=" ";
        this.TalabelName="Description";
        this.SelName = "Select Test";
        this.SellabelName = "--Select one--"
        this.SelValue = ["kitchen,Bedroom,Garage"]

    }

    render(){
        return html`
        <style>
            .form-control{
                width:25%;
            }
        </style>
            <h3>Components</h3>
            <div class="form-control">
                <hi-textbox type="text" .labelName="${this.labelName}" .name="${this.name}" .value="${this.value}" 
                .placeHolder="${this.placeHolder}"></hi-textbox>
            </div>
            <div class="form-control">
                <hi-textarea .TalabelName="${this.TalabelName}" .TaName="${this.TaName}" .Columns="${this.Columns}" .Rows="${this.Rows}"></hi-textarea>
            </div>
            <div class="form-control">
                <hi-select .SelName="${this.SelName}" .SellabelName="${this.SellabelName}">
                    <option .SelValue=${this.SelValue}>${this.SelValue}</option>
                </hi-select>
            </div>
        `;
    }
}

customElements.define('components-app',Components)

我正在寻找的结果是一个select下拉组件,该组件由我作为属性的数组填充。现在,我在下拉列表中什么也得不到,就像它甚至没有在数组中重复以提供选择选项一样。

任何帮助都是最好的。

EN

回答 1

Stack Overflow用户

发布于 2019-05-02 08:43:38

这就是我在hi-select.js中修复它的方法

代码语言:javascript
复制
 <select id="form-field-select" class="form-field__select" name="${this.SelName}">
                    ${repeat(this.SelValue, (i) => html`<option>${i}</option>`)}
                </select>

然后在app.js中这样调用它

代码语言:javascript
复制
<div class="form-control">
                <hi-select .SelName="${this.SelName}" .SellabelName="${this.SellabelName}" .SelValue="${this.SelValue}"></hi-select>
            </div>

现在我的下拉列表正在填充

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55940974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档