【问题标题】:Cannot read property 'property_name' of undefined无法读取未定义的属性“property_name”
【发布时间】:2017-10-18 10:38:01
【问题描述】:

这是我现在的代码:https://jsfiddle.net/5phq111c/5/

HTML部分

<tbody v-for="row in rows" :key="row.product_id">
    <tr>
        <td>
            <select @change="selected" v-model="row.product_id" class="form-control" name="product_id" id="product_id">
                <option value="">Select Product</option>
                <option v-for="item in items" :value="item.id" v-text="item.product_name"></option>
            </select>                                           
        </td>
        <td>
            <textarea type="text" v-model="product.product_details" name="product_details" id="product_details" class="form-control" rows="1" placeholder="Product Details">
            </textarea>
        </td>
        <td>
            <input v-model.number="product.product_sellPrice" type="number" step="any" class="form-control" name="rate" id="rate" placeholder="Rate">
        </td>
    </tr>
</tbody>

Vue JS 部分

export default {    
    data() {
        return {
            rows: [{
                product_id: '',
                product_details: '',
                product_sellPrice: '',      

            }],
        }
    },
    methods: {
        addrow: function (event) {
        event.preventDefault();
        this.rows.push({
            product_id: '',
            product_details: '',
            product_sellPrice: '',
        });
    },
    selected(e) {
        var id = this.row.product_id;
        console.log(id);
        axios.get('/estimate/product/' + id)
        .then((response)=>{
            this.product = '';
            this.product = response.data;
        })
        .catch((error) => {
            console.log(error);
        }); 
    }
}

我想获取选中的product_id,并发送一个axios请求来获取选中的产品的值。我已将 product_id 与该行绑定。我在行对象中获取选定的值,但是当我通过 row.product_id 发送请求时,我收到错误无法读取未定义的属性“product_id”。问题出在哪里?

【问题讨论】:

    标签: vue.js vuejs2


    【解决方案1】:

    您没有定义row 数据属性,而是定义rows 数据属性数组。从v-for 循环内部调用selected 方法不会自动为this.row 赋值。

    如果您想要selected 方法中所选行的product_id,请将其通过@change="selected(row.product_id)" 传递到模板中。

    然后只需在 selected 方法中引用该参数即可:

    selected(id){
      console.log(id);
      axios.get('/estimate/product/' + id)
      ...
    }
    

    【讨论】:

    • 嗯,它正在工作,但如果我添加一个新行,新行将填充前一行数据,并且更改单个正在更改所有其他行数据
    • 这是一个单独的问题,我认为这是因为您正在推送一个 product_id 等于一个空字符串的对象,这就是每个 :key 的绑定由v-for 渲染的元素。但是由于所有键都是相同的值,因此元素将使用来自具有该键的第一个实例的数据呈现。您需要为 key 属性提供 uniqueunchangeing 值。
    • 在这种情况下,如何在这里实现唯一键?这样我就可以使用该键选择每一行并找到值。 "rows[0].quantity" 像这样。
    • 添加一个row_id 属性并在添加行时为其分配一个唯一ID(您可以通过递增计数器row_id: this.counter++ 来实现)。然后使用row_id 作为key 而不是product_id
    【解决方案2】:

    您似乎将rows 声明为一个数组,其中只有一个对象:

        data() {
            return {
                rows: [{
                    product_id: '',
                    product_details: '',
                    product_sellPrice: '',
                }],
    

    但是您尝试像访问常规对象一样访问它:

    var id = this.row.product_id;
    

    此外,您将其声明为rows,但将其访问为row(缺少复数s)。

    要让它运行,首先要决定是要将其命名为rows 还是row,并相应地调整用法。其次,决定rows 是否必须是一个数组(是否应该包含多个对象?)。

    如果它应该是一个数组,你可能想这样访问它:

    var id = this.rows[0].product_id;
    

    如果它不应该是一个数组,就这样声明它:

        data() {
            return {
                rows: {
                    product_id: '',
                    product_details: '',
                    product_sellPrice: '',      
                },
    

    【讨论】:

    • 它将是一个数组。我会像这样推送更多对象 this.rows.push({ product_id: '', product_details: '', product_sellPrice: '', });那么声明 var id = this.rows[?].product_id; 的方式是什么?
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多