【发布时间】:2019-07-20 17:27:37
【问题描述】:
我正在为我的公司制作一个请求管理系统。 要求是:
-能够添加新的请求行。
-选择请求的描述将生成要选择的参数。参数与其各自的描述位于同一行。
-将描述和参数存储为数组。
为了解决这个问题,我们使用 vue.js 在 Laravel 框架的刀片模板中编写脚本。
Vue.component('request', {
props: ["index"],
// Template for every individual row of test
template: `
<tr>
<td>@{{ index }}</td>
<td>
<select :name="description" @change="populate" required>
<option value="" selected disabled>--Select--</option>
@foreach ($types->sortBy('description') as $types)
<option value="{{$types->description}}">{{$types->description}}</option>
@endforeach
</select>
</td>
<td>
<select :name="parameter" required >
<option >@{{ shared.parameter.parameter1 }}</option>
<option >@{{ shared.parameter.parameter2 }}</option>
<option >@{{ shared.parameter.parameter3 }}</option>
</select>
</td>
`,
data(){
return{
// bind the name field of the form, for submission
shared: store,
description: 'tests['+this.index+'][description]',
parameters: 'tests['+this.index+'][parameter]',
something: 2,
}
}
,
methods: {
populate: ()=>{
var self = this.index;
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url:'/parametersByDescription',//this is specified in web routes
type: 'POST',
data: {description: this.description},
success: function(data){
store.parameter = data;
}
})
return;
}
}
})
let store = {
parameter: [],
index 在 root 方法中随函数增加。这样做时还会添加一个新行。添加另一行的整个基础是 vue.component request 中的模板生成大量表单的原因。 populate 函数通过data: 将我的description 发送到URL 指定的控制器中的函数。
这是我开始遇到问题的地方。我需要解析 description 我在 populate 函数的表单中选择的,但是我找不到要使用的具体术语。在 Vue Devtools 中,我可以看到 description 作为数据值之一,但是当我尝试解析 this.description 时,我得到了 Uncaught TypeError: Cannot read property 'description' of undefined。我还将 2 的值硬编码为 something 并尝试解析它,但是出现了相同的错误。我只需要得到这个特定的description 值,其他一切都应该运行顺利。感谢您的宝贵时间。
【问题讨论】:
标签: javascript ajax laravel vue.js