代码之家  ›  专栏  ›  技术社区  ›  rob_hicks

这些bug是用html编写的吗?

  •  0
  • rob_hicks  · 技术社区  · 7 年前

    在lit HTML1.0.0-rc中。2.我有以下模板,无法正常工作。所以,我想我做错了什么。或者这只是lit html中的一个bug?

    import { html } from './node_modules/lit-html/lit-html.js';
    import css from './css.js';
    
    export default function template(c) {
      console.log('props', c.text, c.selected, c.checkbox, c.radioButton);
      const changeText = c.changeText.bind(c);
      return html`
        ${css()}
        <form>
          <div>input cannot be updated programatically</div>
          <input type="text" value="${c.text}" @input="${changeText}"/>
    
          <div>select cannot be set/changed programatically</div>
          <select @change="${ev => {c.selected = ev.currentTarget.value; console.log('value set to', ev.currentTarget.value)}}">
            <option value="" ?selected="${c.selcted === ''}">Select</option>
            <option value="1" ?selected="${c.selcted === '1'}">1</option>
            <option value="2" ?selected="${c.selcted === '2'}">2</option>
            <option value="3" ?selected="${c.selcted === '3'}">3</option>
            <option value="4" ?selected="${c.selcted === '4'}">4</option>
          </select>
          <div>checkbox cannot be updated programatically</div>
          <input
            type="checkbox"
            @change="${(ev) => {c.checkbox = ev.currentTarget.value; console.log('checkbox value:', ev.currentTarget.value)}}"
            ?checked="${c.checkbox === 'on'}"
          />
          <div>radio buttons cannot be updated programatically</div>
          <input
            name="radio"
            type="radio"
            value="1"
            @change="${ev => {c.radioButton = '1'; console.log('radio button value: ', ev.currentTarget.value)}}"
            ?checked="${c.radioButton === '1'}"/>
          <label>1</label>
          <input
            name="radio"
            type="radio"
            value="2"
            @change="${ev => {c.radioButton = '2'; console.log('radio button value: ', ev.currentTarget.value)}}"
            ?checked="${c.radioButton === '2'}"/>
          <label>2</label>
          <input
            name="radio"
            type="radio"
            value="3"
            @change="${ev => {c.radioButton = '3'; console.log('radio button value: ', ev.currentTarget.value)}}"
            ?checked="${c.radioButton === '3'}"/>
          <label>3</label>
        </form>
      `;
    }

    它由以下web组件填充/控制:

    import { render } from './node_modules/lit-html/lit-html.js';
    import template from './template.js';
    
    class MyForm extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({mode: 'open'});
        this.text = 'foo';
        this.selected = '2';
        this.checkbox = 'on';
        this.radioButton = '1';
      }
    
      attributeChangedCallback(name, oVal, nVal) {
    
      }
    
      connectedCallback() {
        this.render();
        setInterval(() => {
          this.text = 'foobar';
          this.selected = '3';
          this.checkbox = 'off';
          this.radioButton = '2';
          this.render();
        }, 2000);
      }
    
      disconnectedCallback() {
    
      }
    
      changeText(ev) {
        const { value } = ev.currentTarget;
        this.text = value;
        this.render();
      }
    
      render() {
        render(template(this), this.shadowRoot);
      }
    
      static get observedAttributes() {
        return ['']
      }
    }
    customElements.get('my-form') || customElements.define('my-form', MyForm);
    
    export { MyForm }

    请注意,web组件试图在第一次渲染时设置各种输入的值。此后,它会尝试使用setInterval再次设置它们。setInterval仅用于显示web组件如何尝试更新模板。

    在选择的情况下,不能通过编程设置选项。对于其他每个输入元素,一旦在UI中选择,就无法通过编程进行更新。

    0 回复  |  直到 7 年前
        1
  •  0
  •   Keith    5 年前

    我不认为这是Lit中的一个bug,尽管你以一种非常独特的方式使用它。

    就你的 <select> 问题是你正在设置 c.selected 但后来检查 c.selcted 在每个 <option> .

    推荐文章