A
A
Andrey Sobolev2020-08-20 16:10:17
Vue.js
Andrey Sobolev, 2020-08-20 16:10:17

Why does Vue disappear after page reload?

Hi all. There is a simple landing page with a form in a block. and vue application is simple
<div id="form">не важно</div>

'use strict';

const DEFAULT_FORM = {
    form: {
        firstname: '',
        lastname: '',
        email: '',
        phone: '',
        company: '',
        partner: '',
        city: '',
        comment: '',
    },
    validation: {
        firstname: true,
        lastname: true,
        email: true,
        phone: true,
        company: true,
        partner: true,
        city: true,
    }
};

new Vue({
    el: '#form',
    data: function () {
        return {
            form: this.cloneObject(DEFAULT_FORM.form),
            validation: this.cloneObject(DEFAULT_FORM.validation),
            isSendForm: false
        };
    },
    computed: {
        isValidForm: function() {
            let valid = this.validation;

            valid.firstname = this.form.firstname.trim().length > 3;
            valid.lastname = this.form.lastname.trim().length > 3;

            let emailExp = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-][email protected][a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
            let regexp = new RegExp(emailExp);
            valid.email = regexp.test(this.form.email);

            let phoneExp = /^\+[\d\s\(\)]{5,}$/;
            regexp = new RegExp(phoneExp);
            valid.phone = regexp.test(this.form.phone);

            valid.company = this.form.company.trim().length > 3;
            valid.partner = this.form.partner.trim().length >= 2;
            valid.city = this.form.city.trim().length >= 2;

            return valid.firstname &&
                    valid.lastname &&
                    valid.email &&
                    valid.phone &&
                    valid.company &&
                    valid.partner &&
                    valid.city;
        },
    },
    methods: {
        sendForm    : function () {
            if (this.isValidForm) {
                let options = {
                    method: 'POST',
                    url: '/send',
                    data: this.toParam(this.form)
                };

                axios(options).then((_json) => {
                    this.isSendForm = true;
                });
            }
        },
        toParam     : function (_object, _prefix) {
            let stack = [];

            for (let key in _object) {
                let value = _object[key];
                key = _prefix ? _prefix + '[' + key + ']' : key;
                let typeValue = (typeof value);

                if (value === null) {
                    value = encodeURIComponent(key) + '=';
                } else if (typeValue !== 'object') {
                    if (typeValue == 'boolean' || typeValue == 'undefined') {
                        value = value ? 1 : 0;
                    } else {
                        if (value == 'true') {
                            value = 1;
                        } else if (value == 'false') {
                            value = 0;
                        }
                        value = encodeURIComponent(value);
                    }
                    value = encodeURIComponent(key) + '=' + value;
                } else {
                    value = this.toParam(value, key);
                }

                stack.push(value);
            }

            return stack.join('&');
        },
        cloneObject : function (_object, _destination = null) {
            let result = JSON.parse(JSON.stringify(_object));

            if (_destination !== null) {
                for (let key in _destination) {
                    delete _destination[key];
                }

                for (let key in result) {
                    _destination[key] = result[key];
                }
            }

            return result;
        },
    },
});


As a result, you load the page all the rules. And VueDevtools in chrome shows that the norms. And as soon as you press F5 and reload the page, VueDevtools shows that there is no Vue and the block with id="form" disappears. (as if v-if). Everything is fine in the console

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sobolev, 2020-08-21
@andrufka46rus

The reason was server side in Nginx

add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

I don't know what effect it has. If anyone knows, please tell

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question