質問編集履歴
1
コード全体とエラーコードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -32,4 +32,200 @@
|
|
32
32
|
#教えて欲しいこと
|
33
33
|
そもそも実現させたいことが間違っているのか、JS構文のミスなのか。
|
34
34
|
いずれはvuexも使用をするつもりですが、今は動きの確認中なのでvuexは使用せずpropsでの動作を試みています。
|
35
|
-
ヒントだけでも教えていただけると助かります。
|
35
|
+
ヒントだけでも教えていただけると助かります。
|
36
|
+
|
37
|
+
#追記
|
38
|
+
実際に動かしているコンポーネントの全体像になります。
|
39
|
+
以下のハードコード時はvueDevツール内のpropsとcomputedには文字列が入っております。
|
40
|
+
|
41
|
+
```vue
|
42
|
+
<template>
|
43
|
+
<div>
|
44
|
+
<div class="head">
|
45
|
+
<h2>User Registration</h2>
|
46
|
+
</div>
|
47
|
+
<ul class="side-bar-list">
|
48
|
+
<SideBar
|
49
|
+
v-for="user in users"
|
50
|
+
:key="user.id"
|
51
|
+
:userData="user"
|
52
|
+
@userEdit="userEdit"
|
53
|
+
></SideBar>
|
54
|
+
</ul>
|
55
|
+
</div>
|
56
|
+
</template>
|
57
|
+
|
58
|
+
<script>
|
59
|
+
import SideBar from './registration_components/SideBar';
|
60
|
+
|
61
|
+
const ApiUrl = 'http://localhost/*******/*******/public/api/items';
|
62
|
+
//====================================================================
|
63
|
+
//ここの部分に問題があると考えています。
|
64
|
+
|
65
|
+
var headers = {
|
66
|
+
'Accept': 'application/json',
|
67
|
+
// 'Authorization': 'Bearer '+'uGI09ICALLdclOBe8UtTvykugiydnoJkcc6pHMmRCi4KhALJShbN2s8KmTdi',//このようにハードコードするとエラーがありません。
|
68
|
+
'Authorization': 'Bearer '.concat(this.tokenNo),
|
69
|
+
};
|
70
|
+
|
71
|
+
//====================================================================
|
72
|
+
let axios = require('axios').create({
|
73
|
+
baseURL: ApiUrl,
|
74
|
+
headers: headers,
|
75
|
+
responseType: 'json'
|
76
|
+
});
|
77
|
+
|
78
|
+
export default {
|
79
|
+
name: 'UserControl',
|
80
|
+
props:{
|
81
|
+
tokenData: {
|
82
|
+
type: String,
|
83
|
+
// required: true
|
84
|
+
}
|
85
|
+
},
|
86
|
+
components: {
|
87
|
+
SideBar
|
88
|
+
},
|
89
|
+
computed:{
|
90
|
+
tokenNo() {
|
91
|
+
return this.tokenData
|
92
|
+
}
|
93
|
+
},
|
94
|
+
data () {
|
95
|
+
return {
|
96
|
+
users: null,
|
97
|
+
editUser: {
|
98
|
+
first_name: '',
|
99
|
+
family_name: '',
|
100
|
+
age: '',
|
101
|
+
email: '',
|
102
|
+
password: '',
|
103
|
+
edit: false
|
104
|
+
}
|
105
|
+
};
|
106
|
+
},
|
107
|
+
mounted () {
|
108
|
+
axios.get(ApiUrl)
|
109
|
+
.then(response => (this.users = response.data));
|
110
|
+
},
|
111
|
+
methods: {
|
112
|
+
// edit method
|
113
|
+
clearEdit () {
|
114
|
+
this.editUser = {
|
115
|
+
first_name: '',
|
116
|
+
family_name: '',
|
117
|
+
age:'',
|
118
|
+
email: '',
|
119
|
+
password: '',
|
120
|
+
edit: false
|
121
|
+
};
|
122
|
+
},
|
123
|
+
// SideBar method
|
124
|
+
userEdit (selectUserID) { // ユーザー編集のONとOFF
|
125
|
+
let selectEditUser = this.users.findIndex(({ id }) => id === selectUserID);// IDから編集を選択されたユーザー取得
|
126
|
+
this.releaseEdit(selectEditUser);
|
127
|
+
this.users[selectEditUser].edit = !this.users[selectEditUser].edit;
|
128
|
+
this.setCurrentUser();
|
129
|
+
this.$forceUpdate();
|
130
|
+
},
|
131
|
+
releaseEdit (userIndex) { // 選択したユーザー以外が編集中だった場合に解除
|
132
|
+
let editTrueIndex = this.users.findIndex(({ edit }) => edit === true);// 編集中のユーザーインデックスを取得
|
133
|
+
if (editTrueIndex > -1) { // indexで取得のためindexが有効かチェック
|
134
|
+
if (this.users[editTrueIndex].edit !== this.users[userIndex].edit) { // 編集中のユーザーと選択したユーザーが違う場合に削除発火
|
135
|
+
this.users[editTrueIndex].edit = false;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
},
|
139
|
+
setCurrentUser () { // 編集ONになった際に右の編集画面に反映させるメソッド
|
140
|
+
let editTrueIndex = this.users.findIndex(({ edit }) => edit === true);
|
141
|
+
if (editTrueIndex > -1) {
|
142
|
+
this.editUser = this.users[editTrueIndex];
|
143
|
+
} else {
|
144
|
+
this.refresh();//編集を更新しない場合はデータベースを再取得
|
145
|
+
this.clearEdit();//右側の記入欄をクリア
|
146
|
+
}
|
147
|
+
},
|
148
|
+
//Api method
|
149
|
+
async update (data) {//ユーザー情報の更新
|
150
|
+
await axios.put(ApiUrl+'/'+data.id, data)
|
151
|
+
.then(
|
152
|
+
alert('user edit ok')
|
153
|
+
)
|
154
|
+
.catch(function (err) {
|
155
|
+
alert(err)
|
156
|
+
});
|
157
|
+
this.refresh()
|
158
|
+
},
|
159
|
+
async addUser () {//Userの追加
|
160
|
+
await axios.post(ApiUrl, this.editUser)
|
161
|
+
.then(
|
162
|
+
alert('user add ok')
|
163
|
+
)
|
164
|
+
.catch(function (err) {
|
165
|
+
alert(err);
|
166
|
+
});
|
167
|
+
this.refresh()//thenの中に書いて不安定になってたけど、ここにして安定した。
|
168
|
+
},
|
169
|
+
async softDelete (id) {//Userの論理削除、Deleted_atにTimeStamp入力
|
170
|
+
await axios.delete(ApiUrl+'/'+id)
|
171
|
+
.then(
|
172
|
+
alert('user softDeleted ok')
|
173
|
+
)
|
174
|
+
.catch(function (err) {
|
175
|
+
alert(err)
|
176
|
+
});
|
177
|
+
this.refresh()
|
178
|
+
},
|
179
|
+
refresh (){//Api処理後の再表示
|
180
|
+
axios.get(ApiUrl)
|
181
|
+
.then(response => (
|
182
|
+
this.users = response.data
|
183
|
+
))
|
184
|
+
.catch(err => (
|
185
|
+
alert(err)
|
186
|
+
));
|
187
|
+
},
|
188
|
+
// Registration method
|
189
|
+
add () {
|
190
|
+
if (this.users.find(editId => editId.id === this.editUser.id)) { //users内にeditUserのidが存在する=>更新の場合
|
191
|
+
if (confirm('ユーザー情報を更新しますか?')) {
|
192
|
+
//↑ var result = confirm('ユーザー情報を更新しますか?')をそのまま代入している。
|
193
|
+
var editTrueIndex = this.users.findIndex(({edit}) => edit === true);// 編集中のインデックス取得
|
194
|
+
this.users[editTrueIndex].edit = !this.users[editTrueIndex].edit; // 編集ボタンを解除
|
195
|
+
this.update(this.users[editTrueIndex]);
|
196
|
+
this.clearEdit();
|
197
|
+
}
|
198
|
+
} else { // users内にeditUserのidが存在しない=>追加の場合
|
199
|
+
if (confirm('ユーザーを追加しますか?')) {
|
200
|
+
this.addUser();
|
201
|
+
this.clearEdit();
|
202
|
+
}
|
203
|
+
}
|
204
|
+
},
|
205
|
+
del () {
|
206
|
+
if (confirm('ユーザーを削除しますか?')) {//user削除
|
207
|
+
var editTrueIndex = this.users.findIndex(({edit}) => edit === true);
|
208
|
+
this.softDelete(this.users[editTrueIndex].id);
|
209
|
+
this.clearEdit();
|
210
|
+
this.refresh();
|
211
|
+
}
|
212
|
+
},
|
213
|
+
}
|
214
|
+
};
|
215
|
+
</script>
|
216
|
+
|
217
|
+
<style scoped lang="scss">
|
218
|
+
|
219
|
+
</style>
|
220
|
+
```
|
221
|
+
methods内でもaxiosは使用していますが、動的な部分ですので初期表示のmountedがメインのエラー個所と考えています。
|
222
|
+
#エラーコード
|
223
|
+
```vue
|
224
|
+
'Authorization': 'Bearer '.concat(this.tokenNo)
|
225
|
+
```
|
226
|
+
上記、使用時のエラーコードは以下になります。
|
227
|
+
```
|
228
|
+
app.js:2947 Uncaught TypeError: Cannot read property 'tokenNo' of undefined
|
229
|
+
```
|
230
|
+
恥ずかしい話、今回の質問後にエラーを確認しましたがundefinedでした。
|
231
|
+
しかし、なぜundefinedになってしまっているのかわかりません。
|