整个数组列表从存储中没有被渲染。

huangapple go评论103阅读模式
英文:

The whole list of the array from store isn't rendered

问题

为什么从存储中的数组列表没有呈现,我只在 div 中获得标题,但没有从存储中获取国家、价格等等。

https://codesandbox.io/s/html-template-forked-ywqs9r?file=/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <script type="module" src="./script.js"></script>
    <title>Vesna</title>
  </head>

  <body>
    <section class="content">
      <div class="content__mid" x-data="">
        <template x-for="item in $store.products.items" :key="item.id">
          <p class="content__mid-item" x-text="item.title"></p>
          <p class="content__mid-item" x-text="item.country"></p>
          <p class="content__mid-item" x-text="item.manufacturer"></p>
          <p class="content__mid-item" x-text="item.price"></p>
          <p class="content__mid-item" x-text="item.validDate"></p>
        </template>
      </div>
    </section>
  </body>
</html>
英文:

Why the whole list of the array from store isn't rendered, I get only the title in the div, but not getting the country, price, etc, from the store.
https://codesandbox.io/s/html-template-forked-ywqs9r?file=/index.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
    &lt;script type=&quot;”module”&quot; src=&quot;./script.js&quot;&gt;&lt;/script&gt;
    &lt;title&gt;Vesna&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;section class=&quot;content&quot;&gt;
      &lt;div class=&quot;content__mid&quot; x-data=&quot;&quot;&gt;
        &lt;template x-for=&quot;item in $store.products.items&quot; :key=&quot;item.id&quot;&gt;
          &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.title&quot;&gt;&lt;/p&gt;
          &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.country&quot;&gt;&lt;/p&gt;
          &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.manufacturer&quot;&gt;&lt;/p&gt;
          &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.price&quot;&gt;&lt;/p&gt;
          &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.validDate&quot;&gt;&lt;/p&gt;
        &lt;/template&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

我不确定这是正确答案,我从未使用过Alpine.js。

我最初注意到,示例在我逐个注释掉每个<p>标记时变得异常,并开始出现错误。然而,通过搜索“Alpine js循环示例”,我注意到在需要在循环中使用多个标记时,它使用了一个包装<div>

也许它的模板不允许在<template>下有多个子元素,有点像React以前需要组件中有一个单一元素一样。

<template x-for="item in $store.products.items" :key="item.id">
  <div>
    <p class="content__mid-item" x-text="item.title"></p>
    <p class="content__mid-item" x-text="item.country"></p>
    <p class="content__mid-item" x-text="item.manufacturer"></p>
    <p class="content__mid-item" x-text="item.price"></p>
    <p class="content__mid-item" x-text="item.validDate"></p>
  </div>
</template>

整个数组列表从存储中没有被渲染。

英文:

I am not sure this is the right answer, I have never used Alpine.js.

I initially noticed that the example got really wonky and started erroring out as I commented out each &lt;p&gt; tag. However, by searching for "Alpine js for loop examples", I noticed that it was using a wrapping div when it needed multiple tags in the loop.

Maybe its templating does not allow multiple children under &lt;template&gt;, sort of like React used to require a single element for a component.

&lt;template x-for=&quot;item in $store.products.items&quot; :key=&quot;item.id&quot;&gt;
  &lt;div&gt;
    &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.title&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.country&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.manufacturer&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.price&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;content__mid-item&quot; x-text=&quot;item.validDate&quot;&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/template&gt;

整个数组列表从存储中没有被渲染。

huangapple
  • 本文由 发表于 2023年3月15日 19:25:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定