SKILL.blade.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. ---
  2. name: inertia-vue-development
  3. description: "Develops Inertia.js v2 Vue client-side applications. Activates when creating Vue pages, forms, or navigation; using <Link>, <Form>, useForm, or router; working with deferred props, prefetching, or polling; or when user mentions Vue with Inertia, Vue pages, Vue forms, or Vue navigation."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Inertia Vue Development
  12. ## Documentation
  13. Use `search-docs` for detailed Inertia v2 Vue patterns and documentation.
  14. ## Basic Usage
  15. ### Page Components Location
  16. Vue page components should be placed in the `{{ $assist->inertia()->pagesDirectory() }}` directory.
  17. ### Page Component Structure
  18. Important: Vue components must have a single root element.
  19. @verbatim
  20. @boostsnippet("Basic Vue Page Component", "vue")
  21. <script setup>
  22. defineProps({
  23. users: Array
  24. })
  25. </script>
  26. <template>
  27. <div>
  28. <h1>Users</h1>
  29. <ul>
  30. <li v-for="user in users" :key="user.id">
  31. {{ user.name }}
  32. </li>
  33. </ul>
  34. </div>
  35. </template>
  36. @endboostsnippet
  37. @endverbatim
  38. ## Client-Side Navigation
  39. ### Basic Link Component
  40. Use `<Link>` for client-side navigation instead of traditional `<a>` tags:
  41. @boostsnippet("Inertia Vue Navigation", "vue")
  42. <script setup>
  43. import { Link } from '@inertiajs/vue3'
  44. </script>
  45. <template>
  46. <div>
  47. <Link href="/">Home</Link>
  48. <Link href="/users">Users</Link>
  49. <Link :href="`/users/${user.id}`">View User</Link>
  50. </div>
  51. </template>
  52. @endboostsnippet
  53. ### Link with Method
  54. @boostsnippet("Link with POST Method", "vue")
  55. <script setup>
  56. import { Link } from '@inertiajs/vue3'
  57. </script>
  58. <template>
  59. <Link href="/logout" method="post" as="button">
  60. Logout
  61. </Link>
  62. </template>
  63. @endboostsnippet
  64. ### Prefetching
  65. Prefetch pages to improve perceived performance:
  66. @boostsnippet("Prefetch on Hover", "vue")
  67. <script setup>
  68. import { Link } from '@inertiajs/vue3'
  69. </script>
  70. <template>
  71. <Link href="/users" prefetch>
  72. Users
  73. </Link>
  74. </template>
  75. @endboostsnippet
  76. ### Programmatic Navigation
  77. @boostsnippet("Router Visit", "vue")
  78. <script setup>
  79. import { router } from '@inertiajs/vue3'
  80. function handleClick() {
  81. router.visit('/users')
  82. }
  83. // Or with options
  84. function createUser() {
  85. router.visit('/users', {
  86. method: 'post',
  87. data: { name: 'John' },
  88. onSuccess: () => console.log('Done'),
  89. })
  90. }
  91. </script>
  92. <template>
  93. <Link href="/users">Users</Link>
  94. <Link href="/logout" method="post" as="button">Logout</Link>
  95. </template>
  96. @endboostsnippet
  97. ## Form Handling
  98. @if($assist->inertia()->hasFormComponent())
  99. ### Form Component (Recommended)
  100. The recommended way to build forms is with the `<Form>` component:
  101. @verbatim
  102. @boostsnippet("Form Component Example", "vue")
  103. <script setup>
  104. import { Form } from '@inertiajs/vue3'
  105. </script>
  106. <template>
  107. <Form action="/users" method="post" #default="{ errors, processing, wasSuccessful }">
  108. <input type="text" name="name" />
  109. <div v-if="errors.name">{{ errors.name }}</div>
  110. <input type="email" name="email" />
  111. <div v-if="errors.email">{{ errors.email }}</div>
  112. <button type="submit" :disabled="processing">
  113. {{ processing ? 'Creating...' : 'Create User' }}
  114. </button>
  115. <div v-if="wasSuccessful">User created!</div>
  116. </Form>
  117. </template>
  118. @endboostsnippet
  119. @endverbatim
  120. ### Form Component With All Props
  121. @verbatim
  122. @boostsnippet("Form Component Full Example", "vue")
  123. <script setup>
  124. import { Form } from '@inertiajs/vue3'
  125. </script>
  126. <template>
  127. <Form
  128. action="/users"
  129. method="post"
  130. #default="{
  131. errors,
  132. hasErrors,
  133. processing,
  134. progress,
  135. wasSuccessful,
  136. recentlySuccessful,
  137. setError,
  138. clearErrors,
  139. resetAndClearErrors,
  140. defaults,
  141. isDirty,
  142. reset,
  143. submit
  144. }"
  145. >
  146. <input type="text" name="name" :value="defaults.name" />
  147. <div v-if="errors.name">{{ errors.name }}</div>
  148. <button type="submit" :disabled="processing">
  149. {{ processing ? 'Saving...' : 'Save' }}
  150. </button>
  151. <progress v-if="progress" :value="progress.percentage" max="100">
  152. {{ progress.percentage }}%
  153. </progress>
  154. <div v-if="wasSuccessful">Saved!</div>
  155. </Form>
  156. </template>
  157. @endboostsnippet
  158. @endverbatim
  159. @if($assist->inertia()->hasFormComponentResets())
  160. ### Form Component Reset Props
  161. The `<Form>` component supports automatic resetting:
  162. - `resetOnError` - Reset form data when the request fails
  163. - `resetOnSuccess` - Reset form data when the request succeeds
  164. - `setDefaultsOnSuccess` - Update default values on success
  165. Use the `search-docs` tool with a query of `form component resetting` for detailed guidance.
  166. @verbatim
  167. @boostsnippet("Form with Reset Props", "vue")
  168. <script setup>
  169. import { Form } from '@inertiajs/vue3'
  170. </script>
  171. <template>
  172. <Form
  173. action="/users"
  174. method="post"
  175. reset-on-success
  176. set-defaults-on-success
  177. #default="{ errors, processing, wasSuccessful }"
  178. >
  179. <input type="text" name="name" />
  180. <div v-if="errors.name">{{ errors.name }}</div>
  181. <button type="submit" :disabled="processing">
  182. Submit
  183. </button>
  184. </Form>
  185. </template>
  186. @endboostsnippet
  187. @endverbatim
  188. @else
  189. Note: This version of Inertia does not support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component. Using these props will cause errors. Upgrade to Inertia v2.2.0+ to use these features.
  190. @endif
  191. Forms can also be built using the `useForm` composable for more programmatic control. Use the `search-docs` tool with a query of `useForm helper` for guidance.
  192. @endif
  193. ### `useForm` Composable
  194. @if($assist->inertia()->hasFormComponent() === false)
  195. For Inertia v2.0.x: Build forms using the `useForm` composable as the `<Form>` component is not available until v2.1.0+.
  196. @else
  197. For more programmatic control or to follow existing conventions, use the `useForm` composable:
  198. @endif
  199. @verbatim
  200. @boostsnippet("useForm Composable Example", "vue")
  201. <script setup>
  202. import { useForm } from '@inertiajs/vue3'
  203. const form = useForm({
  204. name: '',
  205. email: '',
  206. password: '',
  207. })
  208. function submit() {
  209. form.post('/users', {
  210. onSuccess: () => form.reset('password'),
  211. })
  212. }
  213. </script>
  214. <template>
  215. <form @submit.prevent="submit">
  216. <input type="text" v-model="form.name" />
  217. <div v-if="form.errors.name">{{ form.errors.name }}</div>
  218. <input type="email" v-model="form.email" />
  219. <div v-if="form.errors.email">{{ form.errors.email }}</div>
  220. <input type="password" v-model="form.password" />
  221. <div v-if="form.errors.password">{{ form.errors.password }}</div>
  222. <button type="submit" :disabled="form.processing">
  223. Create User
  224. </button>
  225. </form>
  226. </template>
  227. @endboostsnippet
  228. @endverbatim
  229. ## Inertia v2 Features
  230. ### Deferred Props
  231. Use deferred props to load data after initial page render:
  232. @verbatim
  233. @boostsnippet("Deferred Props with Empty State", "vue")
  234. <script setup>
  235. defineProps({
  236. users: Array
  237. })
  238. </script>
  239. <template>
  240. <div>
  241. <h1>Users</h1>
  242. <div v-if="!users" class="animate-pulse">
  243. <div class="h-4 bg-gray-200 rounded w-3/4 mb-2"></div>
  244. <div class="h-4 bg-gray-200 rounded w-1/2"></div>
  245. </div>
  246. <ul v-else>
  247. <li v-for="user in users" :key="user.id">
  248. {{ user.name }}
  249. </li>
  250. </ul>
  251. </div>
  252. </template>
  253. @endboostsnippet
  254. @endverbatim
  255. ### Polling
  256. Use the `usePoll` composable to automatically refresh data at intervals. It handles cleanup on unmount and throttles polling when the tab is inactive.
  257. @boostsnippet("Basic Polling", "vue")
  258. <script setup>
  259. import { usePoll } from '@inertiajs/vue3'
  260. defineProps({
  261. stats: Object
  262. })
  263. usePoll(5000)
  264. </script>
  265. <template>
  266. <div>
  267. <h1>Dashboard</h1>
  268. <div>Active Users: {{ stats.activeUsers }}</div>
  269. </div>
  270. </template>
  271. @endboostsnippet
  272. @boostsnippet("Polling With Request Options and Manual Control", "vue")
  273. <script setup>
  274. import { usePoll } from '@inertiajs/vue3'
  275. defineProps({
  276. stats: Object
  277. })
  278. const { start, stop } = usePoll(5000, {
  279. only: ['stats'],
  280. onStart() {
  281. console.log('Polling request started')
  282. },
  283. onFinish() {
  284. console.log('Polling request finished')
  285. },
  286. }, {
  287. autoStart: false,
  288. keepAlive: true,
  289. })
  290. </script>
  291. <template>
  292. <div>
  293. <h1>Dashboard</h1>
  294. <div>Active Users: {{ stats.activeUsers }}</div>
  295. <button @click="start">Start Polling</button>
  296. <button @click="stop">Stop Polling</button>
  297. </div>
  298. </template>
  299. @endboostsnippet
  300. - `autoStart` (default `true`) — set to `false` to start polling manually via the returned `start()` function
  301. - `keepAlive` (default `false`) — set to `true` to prevent throttling when the browser tab is inactive
  302. ### WhenVisible (Infinite Scroll)
  303. Load more data when user scrolls to a specific element:
  304. @verbatim
  305. @boostsnippet("Infinite Scroll with WhenVisible", "vue")
  306. <script setup>
  307. import { WhenVisible } from '@inertiajs/vue3'
  308. defineProps({
  309. users: Object
  310. })
  311. </script>
  312. <template>
  313. <div>
  314. <div v-for="user in users.data" :key="user.id">
  315. {{ user.name }}
  316. </div>
  317. <WhenVisible
  318. v-if="users.next_page_url"
  319. data="users"
  320. :params="{ page: users.current_page + 1 }"
  321. >
  322. <template #fallback>
  323. <div>Loading more...</div>
  324. </template>
  325. </WhenVisible>
  326. </div>
  327. </template>
  328. @endboostsnippet
  329. @endverbatim
  330. ## Server-Side Patterns
  331. Server-side patterns (Inertia::render, props, middleware) are covered in inertia-laravel guidelines.
  332. ## Common Pitfalls
  333. - Using traditional `<a>` links instead of Inertia's `<Link>` component (breaks SPA behavior)
  334. - Forgetting that Vue components must have a single root element
  335. - Forgetting to add loading states (skeleton screens) when using deferred props
  336. - Not handling the `undefined` state of deferred props before data loads
  337. - Using `<form>` without preventing default submission (use `<Form>` component or `@submit.prevent`)
  338. - Forgetting to check if `<Form>` component is available in your Inertia version