问题文本在升级到Vue 3后没有更新。

huangapple go评论105阅读模式
英文:

problem text not updating after vue 3 upgrade

问题

以下是您要翻译的内容:

在Vue 3中,我有一个组件,我正在尝试重新编写它,更具体地说,我正在使用它们的新设置脚本,以便代码看起来更清晰。这是当前的代码:

export default {
    name: "typeWriter",
    data: () => {
        return {
            typeValue: "",
            typeStatus: false,
            displayTextArray: ['Art', 'Science', 'Math', 'Everything'],
            typingSpeed: 70,
            erasingSpeed: 70,
            newTextDelay: 1000,
            displayTextArrayIndex: 0,
            charIndex: 0,
        };
    },
    created() {
        setTimeout(this.typeText, this.newTextDelay + 200);
    },
    methods: {
        typeText() {
            if (this.charIndex < this.displayTextArray[this.displayTextArrayIndex].length) {
                if (!this.typeStatus) {
                    this.typeStatus = true;
                }

                this.typeValue += this.displayTextArray[this.displayTextArrayIndex].charAt(this.charIndex);
                this.charIndex++;

                setTimeout(this.typeText, this.typingSpeed);
            } else {
                this.typeStatus = false;

                // 停止打字,一旦我们达到希望打字的最后一个内容。
                if (this.displayTextArrayIndex + 1 >= this.displayTextArray.length) {
                    return
                }

                setTimeout(this.eraseText, this.newTextDelay);
            }
        },
        eraseText() {
            if (this.charIndex > 0) {
                if (!this.typeStatus) {
                    this.typeStatus = true;
                }

                this.typeValue = this.displayTextArray[this.displayTextArrayIndex].substring(0, this.charIndex - 1);
                this.charIndex -= 1;

                setTimeout(this.eraseText, this.erasingSpeed);
            } else {
                this.typeStatus = false;
                this.displayTextArrayIndex++;

                setTimeout(this.typeText, this.typingSpeed + 1000);
            }
        },
    },
};

以下是使用<script setup lang="ts">的新Vue 3代码:

let typeValue = ""
let typeStatus = false
let displayTextArray = ['Art', 'Science', 'Math', 'Everything']
let typingSpeed = 70
let erasingSpeed = 70
let newTextDelay = 1000
let displayTextArrayIndex = 0
let charIndex = 0

setTimeout(typeText, newTextDelay);

function typeText() {
    if (charIndex < displayTextArray[displayTextArrayIndex].length) {
        if (!typeStatus) {
            typeStatus = true;
        }

        typeValue += displayTextArray[displayTextArrayIndex].charAt(charIndex);
        charIndex++;

        setTimeout(typeText, typingSpeed);
    } else {
        typeStatus = false;

        // 停止打字,一旦我们达到希望打字的最后一个内容。
        if (displayTextArrayIndex + 1 >= displayTextArray.length) {
            return
        }

        setTimeout(eraseText, newTextDelay);
    }
}

function eraseText() {
    if (charIndex > 0) {
        if (!typeStatus) {
            typeStatus = true;
        }

        typeValue = displayTextArray[displayTextArrayIndex].substring(0, charIndex - 1);
        charIndex -= 1;

        setTimeout(eraseText, erasingSpeed);
    } else {
        typeStatus = false;
        displayTextArrayIndex++;

        setTimeout(typeText, typingSpeed + 1000);
    }
}

我遇到的问题是,在Vue 2代码中,它会正确地更新一个包含typeValue值的div,而新的Vue 3代码不会更新div。我添加了一个console.log,可以看到代码确实正在触发和工作,但是div没有识别到typeValue中的更改,我完全不明白为什么会这样。我对Vue 3仍然很陌生,所以也许我漏掉了什么,但是在我看来,这看起来是正确的,所以我不明白为什么div不像以前那样更新。

英文:

I have a component that I'm trying to rewrite for vue 3, more specifically, using their new setup script so the code is a bit cleaner looking, here is what it looks like currently.

export default {
    name: &quot;typeWriter&quot;,
    data: () =&gt; {
        return {
            typeValue: &quot;&quot;,
            typeStatus: false,
            displayTextArray: [&#39;Art&#39;, &#39;Science&#39;, &#39;Math&#39;, &#39;Everything&#39;],
            typingSpeed: 70,
            erasingSpeed: 70,
            newTextDelay: 1000,
            displayTextArrayIndex: 0,
            charIndex: 0,
        };
    },
    created() {
        setTimeout(this.typeText, this.newTextDelay + 200);
    },
    methods: {
        typeText() {
            if (this.charIndex &lt; this.displayTextArray[this.displayTextArrayIndex].length) {
                if (!this.typeStatus) {
                    this.typeStatus = true;
                }

                this.typeValue += this.displayTextArray[this.displayTextArrayIndex].charAt(this.charIndex);
                this.charIndex++;

                setTimeout(this.typeText, this.typingSpeed);
            } else {
                this.typeStatus = false;

                // stop the typing once we hit the last thing we wish to type.
                if (this.displayTextArrayIndex + 1 &gt;= this.displayTextArray.length) {
                    return
                }

                setTimeout(this.eraseText, this.newTextDelay);
            }
        },
        eraseText() {
            if (this.charIndex &gt; 0) {
                if (!this.typeStatus) {
                    this.typeStatus = true;
                }

                this.typeValue = this.displayTextArray[this.displayTextArrayIndex].substring(0, this.charIndex - 1);
                this.charIndex -= 1;

                setTimeout(this.eraseText, this.erasingSpeed);
            } else {
                this.typeStatus = false;
                this.displayTextArrayIndex++;

                setTimeout(this.typeText, this.typingSpeed + 1000);
            }
        },
    },
};

Here is the new vue 3 code using &lt;script setup lang=&quot;ts&quot;&gt;

let typeValue = &quot;&quot;
let typeStatus = false
let displayTextArray = [&#39;Art&#39;, &#39;Science&#39;, &#39;Math&#39;, &#39;Everything&#39;]
let typingSpeed = 70
let erasingSpeed = 70
let newTextDelay = 1000
let displayTextArrayIndex = 0
let charIndex = 0

setTimeout(typeText, newTextDelay);

function typeText() {
    if (charIndex &lt; displayTextArray[displayTextArrayIndex].length) {
        if (!typeStatus) {
            typeStatus = true;
        }

        typeValue += displayTextArray[displayTextArrayIndex].charAt(charIndex);
        charIndex++;

        setTimeout(typeText, typingSpeed);
    } else {
        typeStatus = false;

        // stop the typing once we hit the last thing we wish to type.
        if (displayTextArrayIndex + 1 &gt;= displayTextArray.length) {
            return
        }

        setTimeout(eraseText, newTextDelay);
    }
}

function eraseText() {
    if (charIndex &gt; 0) {
        if (!typeStatus) {
            typeStatus = true;
        }

        typeValue = displayTextArray[displayTextArrayIndex].substring(0, charIndex - 1);
        charIndex -= 1;

        setTimeout(eraseText, erasingSpeed);
    } else {
        typeStatus = false;
        displayTextArrayIndex++;

        setTimeout(typeText, typingSpeed + 1000);
    }
}

The problem I'm hitting is, the vue 2 code it'll correctly update a div with the value in typeValue, the new vue 3 code is not updating the div, I added a console.log and can see that the code is indeed firing and working, the div just isn't recognizing this change in typeValue and I'm completely lost on why that is.

I'm still new to vue 3, so maybe I missed something, but this looks right to me so I'm lost on why the div isn't updating like it used to.

答案1

得分: 1

以下是您要翻译的内容:

尝试使用 ref 使您的数据响应式:

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    let typeValue = ref("")
    let typeStatus = false
    let displayTextArray = ['Art', 'Science', 'Math', 'Everything']
    let typingSpeed = 70
    let erasingSpeed = 70
    let newTextDelay = 1000
    let displayTextArrayIndex = ref(0)
    let charIndex = 0
    
    setTimeout(typeText, newTextDelay);
    
    function typeText() {
      if (charIndex < displayTextArray[displayTextArrayIndex.value].length) {
        if (!typeStatus) {
          typeStatus = true;
        }
        typeValue.value += displayTextArray[displayTextArrayIndex.value].charAt(charIndex);
        charIndex++;
        setTimeout(typeText, typingSpeed);
      } else {
        typeStatus = false;
        if (displayTextArrayIndex.value + 1 >= displayTextArray.length) {
          return
        }
        setTimeout(eraseText, newTextDelay);
      }
    }

    function eraseText() {
      if (charIndex > 0) {
        if (!typeStatus) {
          typeStatus = true;
        }
        typeValue.value = displayTextArray[displayTextArrayIndex.value].substring(0, charIndex - 1);
        charIndex -= 1;
        setTimeout(eraseText, erasingSpeed);
      } else {
        typeStatus = false;
        displayTextArrayIndex.value++;
        setTimeout(typeText, typingSpeed + 1000);
      }
    }
    return {
      typeValue, eraseText, typeText
    };
  },
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <input type="text" :value="typeValue" />
</div>
英文:

Try to make your data reactive with ref:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { ref } = Vue
const app = Vue.createApp({
setup() {
let typeValue = ref(&quot;&quot;)
let typeStatus = false
let displayTextArray = [&#39;Art&#39;, &#39;Science&#39;, &#39;Math&#39;, &#39;Everything&#39;]
let typingSpeed = 70
let erasingSpeed = 70
let newTextDelay = 1000
let displayTextArrayIndex = ref(0)
let charIndex = 0
setTimeout(typeText, newTextDelay);
function typeText() {
if (charIndex &lt; displayTextArray[displayTextArrayIndex.value].length) {
if (!typeStatus) {
typeStatus = true;
}
typeValue.value += displayTextArray[displayTextArrayIndex.value].charAt(charIndex);
charIndex++;
setTimeout(typeText, typingSpeed);
} else {
typeStatus = false;
if (displayTextArrayIndex.value + 1 &gt;= displayTextArray.length) {
return
}
setTimeout(eraseText, newTextDelay);
}
}
function eraseText() {
if (charIndex &gt; 0) {
if (!typeStatus) {
typeStatus = true;
}
typeValue.value = displayTextArray[displayTextArrayIndex.value].substring(0, charIndex - 1);
charIndex -= 1;
setTimeout(eraseText, erasingSpeed);
} else {
typeStatus = false;
displayTextArrayIndex.value++;
setTimeout(typeText, typingSpeed + 1000);
}
}
return {
typeValue, eraseText, typeText
};
},
})
app.mount(&#39;#demo&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;demo&quot;&gt;
&lt;input type=&quot;text&quot; :value=&quot;typeValue&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 07:04:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定