1

подскажите, где ошибся, вывожу значение из usd и ничего не показывает

<template>
            <div class="box-dop">
                <div class="box">
                    <div class="item full">
                        <div class="checkbox">
                            <input type="checkbox" name="politic" id="politic" value="1" v-model="form.politic">
                            <label for="politic">С  <a href="">Правилами</a>  размещения рекламы полностью согласен(на) {{usd}}</label>
                        </div>
                    </div>
                </div>
                <button type="button" class="btn-blue" @click.prevent="paySystemsShow(activec)">Выбрать способ оплаты</button>
                <div class="box-pay list-pay" :class="{'active':showpay}">
                    <div class="p-item" v-for="paysystem in paysystems">
                        <div class="card">
                            <img :src="'/storage/images/uploads/pay/'+paysystem.icon" :alt="paysystem.title">
                            <span>{{ paySystemCost(paysystem.procent,paysystem.price_dop,paysystem.current) }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        el: '#add_catalog_link',
        data: function(){
            return {
                max: 160,
                maxdescription: 60,
                paysystems: [],
                showpay: false,
                activec: 0,
                sUsd: '',
                form: {
                    url: '',
                    description: '',
                    politic: false,
                    days: 5,
                    cost: 45,
                    status: 0
                }
            }
        },
        computed: {
            costOrder: function() {
                var result = 0,
                    countDays = this.form.days,
                    highlight = this.form.status;
                if (highlight === 0) result = countDays * 9; else result = countDays * 14;
                return result
            },
            usd(){
                console.log(this.sUsd)
                return this.sUsd
            },
        },
        mounted() {
            this.getPaySystems()
            this.paySystemCost()
            this.getUsd()
        },
        methods: {
            getUsd(){
                $.getJSON("https://www.cbr-xml-daily.ru/daily_json.js", function(data) {
                    this.sUsd = data.Valute.USD.Value
                    console.log(this.sUsd)
                });
            },
            getPaySystems(){
                axios.get('/paysystems/getlist')
                    .then((response) => {
                        this.paysystems = response.data
                    })
                    .catch(error => {});
            },
            paySystemsShow(tmp){
                if ( tmp === 0 ){
                    this.showpay = true;
                    this.activec = 1;
                } else {
                    this.showpay = false;
                    this.activec = 0;
                }
            },
            paySystemCost(procent,price,current){

                var sCurrent = null,
                    sCost = 0;
                if (current === 'RUB'){
                    sCurrent = 'RUB'
                    sCost = this.costOrder
                }
                if (current === 'USD'){
                    sCurrent = 'USD'
                    console.log(this.sUsd)
                    //sCost = (this.costOrder / this.currentUsd()).toFixed(2);
                    sCost = this.sUsd;
                }
                if (procent > 0 && price > 0){
                    return sCost + sCurrent+ '(' + price + ' р. + ' + procent + '% )'
                }
                if (procent > 0 && price === 0){
                    return sCost + sCurrent+ '('+ '+ ' + procent + '%)'
                }
                if (procent === 0 && price > 0){
                    return sCost + sCurrent +'('+ price + ' р.)'
                }
                if (procent === 0 && price === 0){
                    return sCost + sCurrent
                }
                //var costProcent = 0;
                //if (procent > 0) costProcent = (this.costOrder * procent) / 100
                //console.log('cost procent - ' + costProcent)
            }
        }
    }

</script>
4

1 ответ 1

1

У вас в обработчике this НЕ экземпляр vue, а что-то другое

$.getJSON("https://www.cbr-xml-daily.ru/daily_json.js", function(data) {
  this.sUsd = data.Valute.USD.Value     // <-- вот здесь
  console.log(this.sUsd)
});

Попробуйте залогировать не this.sUsd, а this и посмотрите что в нём.


Чтобы заработало, надо использовать стрелочную функцию

$.getJSON("https://www.cbr-xml-daily.ru/daily_json.js", (data) => {
  this.sUsd = data.Valute.USD.Value
});

Или сохранить контекст

const vm = this
$.getJSON("https://www.cbr-xml-daily.ru/daily_json.js", function(data) {
  vm.sUsd = data.Valute.USD.Value
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.