Vue 2 Mixin 在 v

编程入门 行业动态 更新时间:2024-10-12 10:26:53
本文介绍了Vue 2 Mixin 在 v-for 中无法正常工作“[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性“discountCalc""“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

#laravel-8 + vue 2

#laravel-8 + vue 2

我做了一个这样的 mixin:mixins/globalMixin.js :

I made a mixin like this : mixins/globalMixin.js :

import Vue from "vue";
Vue.mixin({
    methods: {
        price(item, pricePeriod) {
            return pricePeriod == "monthly"
                ? ((pricePeriod = "monthly"),
                  this.financial(this.priceRates(item["price_m"])))
                : ((pricePeriod = "yearly"),
                  this.financial(this.priceRates(item["price_y"] / 12)));
        },
        priceRates(price, localCurrency) {
            return localCurrency == "EUR"
                ? price / 10.5
                : localCurrency == "USD"
                ? price / 8.9
                : price;
        },
        discountCalc(price, itemCoupon, pricePeriod) {
            return itemCoupon && pricePeriod == "yearly"
                ? this.financial(price - (price * itemCoupon.percentage) / 100)
                : this.financial(price);
        },
        financial(x) {
            return Number.parseFloat(x).toFixed(2);
        }
    }
});

然后我在 app.js 中导入它导入./mixins/globalMixin.js";

Then i imported it in app.js import "./mixins/globalMixin.js";

到现在都还好

当在这样的 vue 模板中使用它时,它可以正常工作

When use it in vue template like this it wokrs properly

<b>{{
    this.discountCalc(
        this.price(item, pricing.period),
        item.coupon,
        localCurrency.name
    )
}} </b>

但是当我像这样在 v-for 中使用它时会出现问题:

But the issue comes up when i use it in v-for like this :

<ul class="list-unstyled mb-3 position-relative">
<li
    v-for="(subItem, index) in JSON.parse(
        item.info
    )"
    :key="index"
>
{{
    this.discountCalc(
        this.price(item, pricing.period),
        item.coupon,
        localCurrency.name
    )
}} 
</li>
</ul>

控制台错误:[Vue 警告]:渲染错误:TypeError:无法读取未定义的属性 'discountCalc'"

the console error : [Vue warn]: Error in render: "TypeError: Cannot read property 'discountCalc' of undefined"

推荐答案

你应该从模板中删除 this :

You should remove this from the template :

<ul class="list-unstyled mb-3 position-relative">
            <li v-for="(subItem, index) in JSON.parse(item.info)" :key="index">
              {{
               discountCalc(
                  price(item, pricing.period),
                  item.coupon,
                  localCurrency.name
                )
              }}
            </li>
          </ul>

这篇关于Vue 2 Mixin 在 v-for 中无法正常工作“[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性“discountCalc""“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 23:09:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Vue   Mixin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!