使用Alpine.data在x-for中访问属性值

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

accessing property values using Alpine.data in a x-for

问题

我正在从API中加载数值并将它们存储在数据对象中,然后将其加载到具有x-init属性的div中。这样我就可以循环遍历数据。它可以正常工作并获取person.name的值,但当我尝试访问person的另一个属性时,什么都不会发生。它只会输出模板中顶部的x-text。

当它们都存在时,为什么我不能同时访问person.name和person.height?

<!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" />

    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.js"></script>
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
  </head>
  <body>
    <div x-data="dropdown" class="wrapper">
      <div x-init="toggle" class="scroll-text">
        <h1>STAR WARS API</h1>
        <template x-for="person in data">
          <div x-text="person.name"></div>
          <div x-text="person.height"></div>
        </template>
      </div>
    </div>

    <script>
      document.addEventListener("alpine:init", () => {
        async function loadJSON(url) {
          const response = await fetch(url);
          return await response.json();
        }

        Alpine.data("dropdown", () => ({
          data: [],
          async toggle() {
            const resp = await loadJSON("https://swapi.dev/api/people/");

            this.data = resp.results;
          },
        }));
      });
    </script>
  </body>
</html>

希望这可以帮助你解决问题。

英文:

I am loading the values from the API and storing them in the data object and loading it into a div with x-init.
So i can loop through the data. It is working and get the values of person.name but when i try to access another property of person nothing happens. It will only output the top x-text in the template.

How come i cant access person.name and person.height at the same time when they both exist?

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

<!-- language: lang-html -->

&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;script defer src=&quot;https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.js&quot;&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
    &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div x-data=&quot;dropdown&quot; class=&quot;wrapper&quot;&gt;
      &lt;div x-init=&quot;toggle&quot; class=&quot;scroll-text&quot;&gt;
        &lt;h1&gt;STAR WARS API&lt;/h1&gt;
        &lt;template x-for=&quot;person in data&quot;&gt;
          &lt;div x-text=&quot;person.name&quot;&gt;&lt;/div&gt;
          &lt;div x-text=&quot;person.height&quot;&gt;&lt;/div&gt;
        &lt;/template&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;script&gt;
      document.addEventListener(&quot;alpine:init&quot;, () =&gt; {
        async function loadJSON(url) {
          const response = await fetch(url);
          return await response.json();
        }

        Alpine.data(&quot;dropdown&quot;, () =&gt; ({
          data: [],
          async toggle() {
            const resp = await loadJSON(&quot;https://swapi.dev/api/people/&quot;);

            this.data = resp.results;
          },
        }));
      });
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

&lt;template&gt; 元素必须只包含一个根元素。因此,请将其包装在外部的 &lt;div&gt; 中,或者像这样重新构建(对于简单文本):

<!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" />

    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.js"></script>
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
  </head>
  <body>
    <div x-data="dropdown" class="wrapper">
      <div x-init="toggle" class="scroll-text">
        <h1>STAR WARS API</h1>
        <template x-for="person in data">
          <div x-text="`${person.name} - ${person.height} cm`"></div>
        </template>
      </div>
    </div>

    <script>
      document.addEventListener("alpine:init", () => {
        async function loadJSON(url) {
          const response = await fetch(url);
          return await response.json();
        }

        Alpine.data("dropdown", () => ({
          data: [],
          async toggle() {
            const resp = await loadJSON("https://swapi.dev/api/people/");

            this.data = resp.results;
          },
        }));
      });
    </script>
  </body>
</html>
英文:

The &lt;template&gt; element must contain only one root element. So wrap it with e.g. an outer &lt;div&gt; or restructure a bit like this (for simple text):

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

<!-- language: lang-html -->

&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;script defer src=&quot;https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.js&quot;&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
    &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div x-data=&quot;dropdown&quot; class=&quot;wrapper&quot;&gt;
      &lt;div x-init=&quot;toggle&quot; class=&quot;scroll-text&quot;&gt;
        &lt;h1&gt;STAR WARS API&lt;/h1&gt;
        &lt;template x-for=&quot;person in data&quot;&gt;
          &lt;div x-text=&quot;`${person.name} - ${person.height} cm`&quot;&gt;&lt;/div&gt;
        &lt;/template&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;script&gt;
      document.addEventListener(&quot;alpine:init&quot;, () =&gt; {
        async function loadJSON(url) {
          const response = await fetch(url);
          return await response.json();
        }

        Alpine.data(&quot;dropdown&quot;, () =&gt; ({
          data: [],
          async toggle() {
            const resp = await loadJSON(&quot;https://swapi.dev/api/people/&quot;);

            this.data = resp.results;
          },
        }));
      });
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定