浏览代码

clarify execution order of `init` (#3702)

there are multiple ways to run code on Alpine initialization, so this clarifies the order of execution of the object `init()` method vs the `x-init` directive.
Andrew Brown 1 年之前
父节点
当前提交
d77d117bc6
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      packages/docs/src/en/directives/init.md

+ 15 - 0
packages/docs/src/en/directives/init.md

@@ -72,3 +72,18 @@ Alpine.data('dropdown', () => ({
     },
 }))
 ```
+
+If you have both an `x-data` object containing an `init()` method and an `x-init` directive, the `x-data` method will be called before the directive.
+
+```alpine
+<div
+    x-data="{
+        init() {
+            console.log('I am called first')
+        }
+    }"
+    x-init="console.log('I am called second')"
+    >
+    ...
+</div>
+```