使用 `x-init` 和一个 setter 来更新 `x-data` 变量,并在前端反映出来。

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

use x-init and a setter to update x-data variable and reflect it in the frontend

问题

在初始化时调用函数以更改 x-data 变量并在前端反映它。在控制台中,该值为false,但在前端中,x-text 和 x-if 仍然会像它是true一样响应。如何使前端反映更改?

<!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" />
    <title>Document</title>
    <script src="//unpkg.com/alpinejs" defer></script>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div x-data="{ val: true }" x-init="setVal()">
      <div><span x-text="val"></span></div>
      <template x-if="val">
        <div>Renders on x-if directive.</div>
      </template>
    </div>

    <script>
      function setVal() {
        this.val = false;
        console.log("val", this.val);
      }
    </script>
  </body>
</html>
英文:

I am trying to call a function on init to change a x-data variable and reflect it in the frontend.
In the console the value is false but in the frontend the x-text and the x-if still react as if is true.
How do i get the frontend to reflect the change?

<!-- 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;title&gt;Document&lt;/title&gt;
    &lt;script src=&quot;//unpkg.com/alpinejs&quot; defer&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div x-data=&quot;{ val: true }&quot; x-init=&quot;setVal()&quot;&gt;
      &lt;div&gt;&lt;span x-text=&quot;val&quot;&gt;&lt;/span&gt;&lt;/div&gt;
      &lt;template x-if=&quot;val&quot;&gt;
        &lt;div&gt;Renders on x-if directive.&lt;/div&gt;
      &lt;/template&gt;
    &lt;/div&gt;

    &lt;script&gt;
      function setVal() {
        this.val = false;
        console.log(&quot;val&quot;, this.val);
      }
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

你可以直接在x-init指令内部改变状态,或者将setVal()函数放在Alpine.js上下文中,以便它可以访问和修改数据。

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

<!-- language: lang-html -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>

<div x-data="{ val: true }" x-init="val = false">
  <div><span x-text="`val的值: ${val}`"></span></div>
</div>

<div x-data="{ val: true, setVal() { this.val = false } }" x-init="setVal()">
  <div><span x-text="`val的值: ${val}`"></span></div>
</div>

<!-- end snippet -->
英文:

You can mutate a state directly inside x-init directive, or put setVal() function inside the Alpine.js context, so it can access and mutate the data.

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

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

&lt;script defer src=&quot;https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js&quot;&gt;&lt;/script&gt;

&lt;div x-data=&quot;{ val: true }&quot; x-init=&quot;val = false&quot;&gt;
  &lt;div&gt;&lt;span x-text=&quot;`Value of val: ${val}`&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div x-data=&quot;{ val: true, setVal() {this.val = false} }&quot; x-init=&quot;setVal()&quot;&gt;
  &lt;div&gt;&lt;span x-text=&quot;`Value of val: ${val}`&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定