我用Vue.js将数据发送到一个基本且非常简单的API,该API将邮箱地址订阅到下面的新闻稿via,并显示一些消息(状态和错误).

我特别使用指令v-if和v-for来显示错误和两条消息.问题是当页面加载时,这两个段落元素和无序列表元素"闪烁",直到页面完全加载或直到Vue.js实例被挂载.

我做错了吗?这是一个普遍的问题吗?我能做些什么来防止所有这些元素在加载时闪烁?

提前谢谢你.

以下是HTML:

<div id="subscribe-to-newsletter">
    <form v-on:submit.prevent="storeSubscriber" novalidate>
        <label for="email-address">E-mail address</label>
            <input type="email" name="email_address" id="email-address" placeholder="exemple@nomdedomaine.fr" v-model="emailAddress">
            <input type="submit" name="submit" value="Rester informé">
    </form>

    <p v-if="success">Thank you, we will keep you updated</p>

    <p v-if="loading">Loading</p>

    <ul v-for="error in errors.email_address">
        <li v-text="error"></li>
    </ul>
</div>

下面是JavaScript:

const subscribe = new Vue({
    el: '#subscribe-to-newsletter',

    data: {
        emailAddress: '',
        loading: false,
        success: false,
        errors: []
    },

    methods: {
        storeSubscriber() {
            subscribe.success = false;
            subscribe.errors = [];
            subscribe.loading = true;

            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/api/subscriber', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

            xhr.onreadystatechange = function () {
                if (this.readyState == 4) {
                    subscribe.loading = false;

                    if (this.status == 200) {
                        subscribe.success = true;
                    } else {
                        subscribe.errors = JSON.parse(xhr.responseText);
                    }
                }
            }

            xhr.send(encodeURI('email_address=' + subscribe.emailAddress));
        },
    }
})

Edit

我试图使用v-Clope指令,但它并没有解决问题

JSFiddle of the problem

Solution

将此添加到CSS中

[v-cloak] {
    display: none;
}

下面是新的HTML:

<div id="subscribe-to-newsletter">
    <form v-on:submit.prevent="storeSubscriber" novalidate>
        <label for="email-address">E-mail address</label>
            <input type="email" name="email_address" id="email-address" placeholder="exemple@nomdedomaine.fr" v-model="emailAddress">
            <input type="submit" name="submit" value="Rester informé">
    </form>

    <p v-if="success" v-cloak>Thank you, we will keep you updated</p>

    <p v-if="loading" v-cloak>Loading</p>

    <ul v-for="error in errors.email_address" v-cloak>
        <li v-text="error"></li>
    </ul>
</div>

推荐答案

v-cloak就可以解决问题.

https://vuejs.org/v2/api/#v-cloak

Vue.js相关问答推荐

复选框列未在vutify中排序

在VITE-VUE APP-DEV模式上重定向HTTP请求

使用V-TOOLTIP(实例化)使工具提示在V-文本字段中显示清晰的图标

在移动版本中使用标准的 v-data-table

Vuejs - 更新数组中对象的数组

使用数组元素的计算(computed)属性

如何在 Vue.js 中添加动态组件/部分

在没有组件的vue js中拖放

将 props 传递给 Vue 组件中的元素属性

在渲染组件之前从 api 获取数据

如何在 Vue 3 中使用 SSR

全局禁用 vuetify 组件的涟漪效应

使用vue.js单击时如何获取按钮的值

基于媒体查询的vue绑定值

将 VueJS 数据属性重置为初始值

输入文件 Select 事件在 Select 同一文件时未触发

Vue JS 在渲染前等待数据

Vue CLI 3 sass-resources-loader - Options.loaders 未定义

Vue在按钮单击时添加一个组件

找不到模块 'babel-core' 但已安装 @babel/core