Skip to content

Commit 19e99c4

Browse files
authored
fix(vue): Update data setup guide for Vue, remove tsx (#7727)
1 parent 5ba9551 commit 19e99c4

File tree

1 file changed

+160
-3
lines changed
  • src/pages/[platform]/build-a-backend/data/set-up-data

1 file changed

+160
-3
lines changed

src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx

Lines changed: 160 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ First, install the Amplify client library to your project:
127127
```bash title="Terminal" showLineNumbers={false}
128128
npm add aws-amplify
129129
```
130+
</InlineFilter>
131+
132+
<InlineFilter filters={["react", "angular", "javascript", "nextjs", "react-native"]}>
130133

131134
In your app's entry point, typically **main.tsx** for React apps created using Vite, make the following edits:
132135

@@ -139,6 +142,20 @@ Amplify.configure(outputs);
139142

140143
</InlineFilter>
141144

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+
142159
<InlineFilter filters={["android"]}>
143160

144161
Under Gradle Scripts, open build.gradle (Module :app), add the following lines:
@@ -360,10 +377,15 @@ Future<void> main() async {
360377
</InlineFilter>
361378
## Write data to your backend
362379

363-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
380+
<InlineFilter filters={["react", "angular", "javascript", "nextjs", "react-native"]}>
364381

365382
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.
366383

384+
</InlineFilter>
385+
386+
387+
<InlineFilter filters={["react", "angular", "javascript", "nextjs", "react-native"]}>
388+
367389
```tsx title="src/TodoList.tsx"
368390
import type { Schema } from '../amplify/data/resource'
369391
import { generateClient } from 'aws-amplify/data'
@@ -384,6 +406,38 @@ export default function TodoList() {
384406
}
385407
```
386408

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+
387441
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.
388442

389443
<Callout>
@@ -583,7 +637,7 @@ Creating Todo successful.
583637

584638
Next, list all your todos and then refetch the todos after a todo has been added:
585639

586-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
640+
<InlineFilter filters={["react", "angular", "javascript", "nextjs", "react-native"]}>
587641

588642

589643
```tsx title="src/TodoList.tsx"
@@ -627,6 +681,54 @@ export default function TodoList() {
627681
}
628682
```
629683

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+
630732
</InlineFilter>
631733
<InlineFilter filters={["android"]}>
632734

@@ -838,7 +940,7 @@ class _MyHomePageState extends State<MyHomePage> {
838940

839941
## Subscribe to real-time updates
840942

841-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
943+
<InlineFilter filters={["react", "angular", "javascript", "nextjs", "react-native"]}>
842944

843945
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.
844946

@@ -884,6 +986,61 @@ export default function TodoList() {
884986
}
885987
```
886988

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+
8871044
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.
8881045

8891046
<Callout>

0 commit comments

Comments
 (0)