|
1 | | -<script> |
2 | | -export default { |
3 | | - name: 'Example3', |
4 | | - data() { |
5 | | - return { |
6 | | - form: { |
7 | | - name: '' |
8 | | - }, |
9 | | - response: '—', |
10 | | - error: null |
11 | | - } |
12 | | - }, |
13 | | - methods: { |
14 | | - async helloNamePost(name) { |
15 | | - try { |
16 | | - const res = await this.$axios.$post( |
17 | | - '/.netlify/functions/hello-name-post', |
18 | | - { name } |
19 | | - ) |
20 | | - this.response = res |
21 | | - this.error = null |
22 | | - } catch (e) { |
23 | | - this.error = e.response |
24 | | - this.response = '—' |
25 | | - } |
26 | | - }, |
27 | | - async helloNamePostError(name) { |
28 | | - try { |
29 | | - const res = await this.$axios.$get( |
30 | | - `/.netlify/functions/hello-name-post?name=${name}` |
31 | | - ) |
32 | | - this.response = res |
33 | | - this.error = null |
34 | | - } catch (e) { |
35 | | - this.error = e.response |
36 | | - this.response = '—' |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | -} |
41 | | -</script> |
42 | | - |
43 | | -<template> |
44 | | - <ElForm |
45 | | - ref="form" |
46 | | - :model="form" |
47 | | - @submit.native.prevent="helloNamePost(form.name)" |
48 | | - inline |
49 | | - label-width="auto" |
50 | | - label-position="left" |
51 | | - > |
52 | | - <h2>3. Hello, {name} (POST version)</h2> |
53 | | - <ElFormItem label="Name"> |
54 | | - <ElInput |
55 | | - v-model="form.name" |
56 | | - placeholder="Your name" |
57 | | - data-cy="input-hello-name-post" |
58 | | - /> |
59 | | - </ElFormItem> |
60 | | - <ElButton |
61 | | - @click="helloNamePost(form.name)" |
62 | | - type="primary" |
63 | | - data-cy="btn-hello-name-post" |
64 | | - > |
65 | | - 👋 Say hello |
66 | | - </ElButton> |
67 | | - <ElButton |
68 | | - @click="helloNamePostError(form.name)" |
69 | | - type="danger" |
70 | | - data-cy="btn-hello-name-post-error" |
71 | | - > |
72 | | - .$get() Error |
73 | | - </ElButton> |
74 | | - <p>Response: {{ response }}</p> |
75 | | - <p v-if="error" style="color:red;"> |
76 | | - <strong>Error {{ error.status }}</strong> |
77 | | - <br /> |
78 | | - {{ error.data }} |
79 | | - </p> |
80 | | - </ElForm> |
81 | | -</template> |
| 1 | +<script> |
| 2 | +export default { |
| 3 | + name: 'Example3', |
| 4 | + data() { |
| 5 | + return { |
| 6 | + form: { |
| 7 | + name: '', |
| 8 | + }, |
| 9 | + response: '—', |
| 10 | + error: null, |
| 11 | + } |
| 12 | + }, |
| 13 | + methods: { |
| 14 | + async helloNamePost(name) { |
| 15 | + try { |
| 16 | + const res = await this.$axios.$post( |
| 17 | + '/.netlify/functions/hello-name-post', |
| 18 | + { name } |
| 19 | + ) |
| 20 | + this.response = res |
| 21 | + this.error = null |
| 22 | + } catch (e) { |
| 23 | + this.error = e.response |
| 24 | + this.response = '—' |
| 25 | + } |
| 26 | + }, |
| 27 | + async helloNamePostError(name) { |
| 28 | + try { |
| 29 | + const res = await this.$axios.$get( |
| 30 | + `/.netlify/functions/hello-name-post?name=${name}` |
| 31 | + ) |
| 32 | + this.response = res |
| 33 | + this.error = null |
| 34 | + } catch (e) { |
| 35 | + this.error = e.response |
| 36 | + this.response = '—' |
| 37 | + } |
| 38 | + }, |
| 39 | + }, |
| 40 | +} |
| 41 | +</script> |
| 42 | + |
| 43 | +<template> |
| 44 | + <ElForm |
| 45 | + ref="form" |
| 46 | + :model="form" |
| 47 | + inline |
| 48 | + label-width="auto" |
| 49 | + label-position="left" |
| 50 | + @submit.native.prevent="helloNamePost(form.name)" |
| 51 | + > |
| 52 | + <h2>3. Hello, {name} (POST version)</h2> |
| 53 | + <ElFormItem label="Name"> |
| 54 | + <ElInput |
| 55 | + v-model="form.name" |
| 56 | + placeholder="Your name" |
| 57 | + data-cy="input-hello-name-post" |
| 58 | + /> |
| 59 | + </ElFormItem> |
| 60 | + <ElButton |
| 61 | + type="primary" |
| 62 | + data-cy="btn-hello-name-post" |
| 63 | + @click="helloNamePost(form.name)" |
| 64 | + > |
| 65 | + 👋 Say hello |
| 66 | + </ElButton> |
| 67 | + <ElButton |
| 68 | + type="danger" |
| 69 | + data-cy="btn-hello-name-post-error" |
| 70 | + @click="helloNamePostError(form.name)" |
| 71 | + > |
| 72 | + .$get() Error |
| 73 | + </ElButton> |
| 74 | + <p>Response: {{ response }}</p> |
| 75 | + <p v-if="error" style="color:red;"> |
| 76 | + <strong>Error {{ error.status }}</strong> |
| 77 | + <br /> |
| 78 | + {{ error.data }} |
| 79 | + </p> |
| 80 | + </ElForm> |
| 81 | +</template> |
0 commit comments