【问题标题】:Passing an array using Stencil JS, and it is not rendering on screen, but displays in the console使用 Stencil JS 传递数组,它不在屏幕上呈现,而是在控制台中显示
【发布时间】:2022-10-24 09:22:29
【问题描述】:
    export class ArraySample {
          @Prop({ mutable: true }) arraydata: string;
          @State() _arraydata: { name: string; value: string }[] = [];
          @State() userInput: string
    
          componentWillLoad() {
            this.parseOptions();
          }
    
          @Watch("arraydata")
          parseOptions() {
            if (this.arraydata) {
            this._arraydata = JSON.parse(this.arraydata);
          }
      }

这是我面临问题的地方。我正在映射数组,但无法得到结果。

      render() {
        return (
          <Host>
            <div>{this._arraydata.length}</div>
            {this._arraydata.map((x) => {
              <div>
                <h1>{x.name}</h1>
              </div>;
              console.log(x.name);
            })}
          </Host>
        );
      }
    }

【问题讨论】:

    标签: stenciljs


    【解决方案1】:

    我尝试使用您的代码并传递一个数组来呈现组件。我做了一些细微的改变,它的工作原理。希望您能够找到代码中遗漏的内容,因为它看起来很简单。

    import {
      Component,
      Prop,
      h,
      State,
      Watch,
      Host
    } from '@stencil/core';
    
    @Component({
      tag: 'my-component',
      styleUrl: 'my-component.css',
      shadow: true,
    })
    export class MyComponent {
      @Prop({
        mutable: true
      }) arraydata: string;
    
      @State() _arraydata: Array < {
        name: string;value: string
      } > = [];
      @State() userInput: string
    
      componentWillLoad() {
        this.parseOptions();
      }
    
      @Watch("arraydata")
      parseOptions(): Array < string > {
        if (this.arraydata) {
          this._arraydata = JSON.parse(this.arraydata);
        }
        return []
      }
    
      render() {
          return ( < Host > {
              this._arraydata.length
            } | {
              this._arraydata.map(item => item)
            } < /Host>)}
          }
    &lt;my-component arrayData="[1,2,3,4,5]"&gt;&lt;/my-component&gt;

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-01-23
    • 2022-01-27
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多