我使用vue.js的目标是在出现确认提示之前更改HTML.

index.html

    <div id='vueexample'>
        <button @click="colorFunction()">Click me!</button>
    <p v-if="userIsDeciding" style="background-color: yellow;">The user has to decide.</p>
</div>
<script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js'></script>
<script>
new Vue({
    el: '#vueexample',
    data: { userIsDeciding: false },
    methods: {
        colorFunction: function() {             
            this.userIsDeciding = true; 
      if(confirm("please confirm")){
        //some stuff
      } 
            this.userIsDeciding = false;    
        }
    }
});
</script>

(Here is the fiddle.)

问题是:在确认提示符之前没有显示P标记.

我的问题是:如何使p-tag在确认提示符前显示?

任何帮助我都很感激,❤️

推荐答案

您可以try 使用setTimeout函数:

new Vue({
    el: '#vueexample',
    data() { 
    return { userIsDeciding: false }
  },
    methods: {
        colorFunction: function() {             
            this.userIsDeciding = true; 
      setTimeout(() => {
        if(confirm("please confirm")){
        //some stuff
        }   
        this.userIsDeciding = false;
      }, 0)
        }
    }
});
<script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js'></script>
<div id='vueexample'>
        <button @click="colorFunction()">Click me!</button>
    <p v-if="userIsDeciding" style="background-color: yellow;">The user has to decide.</p>
</div>

Vue.js相关问答推荐

使用nuxt I18N验证规则消息翻译

用于循环数组的模板元素内的条件渲染

如何默认打开一个v-list-group?

为什么我在 Vue npm run serve 的 localhost 中得到无法获取

TailwindCSS 为什么前者比后者重要?

使用 Vue.js 清除数组内容和react性问题

Vue-tables-2(vuex)react性不起作用

将 Vue 应用程序挂载到主 Vue 应用程序的容器中

Express.js + lint 出错

VueJS + VueRouter:有条件地禁用路由

Nuxt & Vuetify:如何控制 CSS 文件的加载顺序?

错误:[vuex] 期望字符串作为类型,但发现未定义

在组件外使用 VueI18n 的问题

阻止 Vue 积极重用 dom 元素

基本 vue.js 2 和 vue-resource http 获取变量赋值

将 SignalR 与 Vue.js 和 Vuex 集成

未在实例上定义,但在渲染期间引用

在 Vuejs 中放置 firebase.auth().onAuthStateChanged() 的位置

v-for 中的计算(Computed)/动态(Dynamic) v-model 名称

Vue.js 中的条件 依赖于props值?