@@ -127,6 +127,9 @@ First, install the Amplify client library to your project:
127
127
``` bash title="Terminal" showLineNumbers={false}
128
128
npm add aws-amplify
129
129
```
130
+ </InlineFilter >
131
+
132
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
130
133
131
134
In your app's entry point, typically ** main.tsx** for React apps created using Vite, make the following edits:
132
135
@@ -139,6 +142,20 @@ Amplify.configure(outputs);
139
142
140
143
</InlineFilter >
141
144
145
+ <InlineFilter filters = { [" vue" ]} >
146
+
147
+ In your app's entry point, typically ** main.ts** for Vue apps created using Vite, make the following edits:
148
+
149
+ ``` tsx title="src/main.ts"
150
+ import { Amplify } from ' aws-amplify' ;
151
+ import outputs from ' ../amplify_outputs.json' ;
152
+
153
+ Amplify .configure (outputs );
154
+ ```
155
+
156
+ </InlineFilter >
157
+
158
+
142
159
<InlineFilter filters = { [" android" ]} >
143
160
144
161
Under Gradle Scripts, open build.gradle (Module :app), add the following lines:
@@ -360,10 +377,15 @@ Future<void> main() async {
360
377
</InlineFilter >
361
378
## Write data to your backend
362
379
363
- <InlineFilter filters = { [" react" , " angular" , " javascript" , " vue " , " nextjs" , " react-native" ]} >
380
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
364
381
365
382
Let's first add a button to create a new todo item. To make a "create Todo" API request, generate the data client using ` generateClient() ` in your frontend code, and then call ` .create() ` operation for the Todo model. The Data client is a fully typed client that gives you in-IDE code completion. To enable this in-IDE code completion capability, pass in the ` Schema ` type to the ` generateClient ` function.
366
383
384
+ </InlineFilter >
385
+
386
+
387
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
388
+
367
389
``` tsx title="src/TodoList.tsx"
368
390
import type { Schema } from ' ../amplify/data/resource'
369
391
import { generateClient } from ' aws-amplify/data'
@@ -384,6 +406,38 @@ export default function TodoList() {
384
406
}
385
407
```
386
408
409
+ </InlineFilter >
410
+
411
+
412
+ <InlineFilter filters = { [" vue" ]} >
413
+
414
+ ``` html title="src/TodoList.vue"
415
+ <script setup lang =" ts" >
416
+ import type { Schema } from ' ../../amplify/data/resource'
417
+ import { generateClient } from ' aws-amplify/data'
418
+
419
+ const client = generateClient< Schema> ()
420
+
421
+ async function createTodo () {
422
+ await client .models .Todo .create ({
423
+ content: window .prompt (" Todo content?" ),
424
+ isDone: false
425
+ })
426
+ }
427
+ </script >
428
+
429
+ <template >
430
+ <div >
431
+ <button @click =" createTodo" >Add new todo</button >
432
+ </div >
433
+ </template >
434
+ ```
435
+
436
+ </InlineFilter >
437
+
438
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
439
+
440
+
387
441
Run the application in local development mode and check your network tab after creating a todo. You should see a successful request to a ` /graphql ` endpoint.
388
442
389
443
<Callout >
@@ -583,7 +637,7 @@ Creating Todo successful.
583
637
584
638
Next, list all your todos and then refetch the todos after a todo has been added:
585
639
586
- <InlineFilter filters = { [" react" , " angular" , " javascript" , " vue " , " nextjs" , " react-native" ]} >
640
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
587
641
588
642
589
643
``` tsx title="src/TodoList.tsx"
@@ -627,6 +681,54 @@ export default function TodoList() {
627
681
}
628
682
```
629
683
684
+ </InlineFilter >
685
+
686
+ <InlineFilter filters = { [" vue" ]} >
687
+
688
+ ``` html title="src/TodoList.vue"
689
+ <script setup lang =" ts" >
690
+ import { onMounted , ref } from ' vue' ;
691
+ import type { Schema } from ' ../../amplify/data/resource'
692
+ import { generateClient } from ' aws-amplify/data'
693
+
694
+ const client = generateClient< Schema> ()
695
+
696
+ // create a reactive reference to the array of todos
697
+ const todos = ref< Array < Schema[' Todo' ][' type' ]>> ([]);
698
+
699
+ function fetchTodos () {
700
+ const { data: items , errors } = await client .models .Todo .list ();
701
+ todos .value = items;
702
+ }
703
+
704
+ async function createTodo () {
705
+ await client .models .Todo .create ({
706
+ content: window .prompt (" Todo content?" ),
707
+ isDone: false
708
+ })
709
+ fetchTodos ();
710
+ }
711
+
712
+ onMounted (() => {
713
+ fetchTodos ();
714
+ });
715
+
716
+ </script >
717
+
718
+ <template >
719
+ <div >
720
+ <button @click =" createTodo" >Add new todo</button >
721
+ <ul >
722
+ <li
723
+ v-for =" todo in todos"
724
+ :key =" todo.id" >
725
+ {{ todo.content }}
726
+ </li >
727
+ </ul >
728
+ </div >
729
+ </template >
730
+ ```
731
+
630
732
</InlineFilter >
631
733
<InlineFilter filters = { [" android" ]} >
632
734
@@ -838,7 +940,7 @@ class _MyHomePageState extends State<MyHomePage> {
838
940
839
941
## Subscribe to real-time updates
840
942
841
- <InlineFilter filters = { [" react" , " angular" , " javascript" , " vue " , " nextjs" , " react-native" ]} >
943
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
842
944
843
945
You can also use ` observeQuery ` to subscribe to a live feed of your backend data. Let's refactor the code to use a real-time observeQuery instead.
844
946
@@ -884,6 +986,61 @@ export default function TodoList() {
884
986
}
885
987
```
886
988
989
+ </InlineFilter >
990
+
991
+ <InlineFilter filters = { [" vue" ]} >
992
+ ``` html title="src/TodoList.vue"
993
+ <script setup lang =" ts" >
994
+ import { onMounted , ref } from ' vue' ;
995
+ import type { Schema } from ' ../../amplify/data/resource'
996
+ import { generateClient } from ' aws-amplify/data'
997
+
998
+ const client = generateClient< Schema> ()
999
+
1000
+ // create a reactive reference to the array of todos
1001
+ const todos = ref< Array < Schema[' Todo' ][" type" ]>> ([]);
1002
+
1003
+ function fetchTodos () {
1004
+ client .models .Todo .observeQuery ().subscribe ({
1005
+ next : ({ items, isSynced }) => {
1006
+ todos .value = items
1007
+ },
1008
+ });
1009
+ }
1010
+
1011
+ async function createTodo () {
1012
+ await client .models .Todo .create ({
1013
+ content: window .prompt (" Todo content?" ),
1014
+ isDone: false
1015
+ })
1016
+ // no more manual refetchTodos required!
1017
+ // - fetchTodos()
1018
+ }
1019
+
1020
+ onMounted (() => {
1021
+ fetchTodos ();
1022
+ });
1023
+
1024
+ </script >
1025
+
1026
+ <template >
1027
+ <div >
1028
+ <button @click =" createTodo" >Add new todo</button >
1029
+ <ul >
1030
+ <li
1031
+ v-for =" todo in todos"
1032
+ :key =" todo.id" >
1033
+ {{ todo.content }}
1034
+ </li >
1035
+ </ul >
1036
+ </div >
1037
+ </template >
1038
+ ```
1039
+
1040
+ </InlineFilter >
1041
+
1042
+ <InlineFilter filters = { [" react" , " angular" , " javascript" , " nextjs" , " react-native" ]} >
1043
+
887
1044
Now try to open your app in two browser windows and see how creating a todo in one window automatically adds the todo in the second window as well.
888
1045
889
1046
<Callout >
0 commit comments