我一直在努力使用 Vue 和 MathLive 来处理排版随机生成的数字及其正方形。程序的功能是生成一个1到35的随机整数,计算平方,用MathLive排版。有两个按钮可以将一个添加到整数或创建另一个随机按钮。我排版初始值没有问题,但是当我创建一个不同的整数或在页面上加 1 时,它永远不会重新排版。我正在尝试将此程序作为 Vue 中的一个组件来实现。这是我的 MWE(仅限组件):
<template lang="html">
<div class="problem">
<p id="math">$${{num}}^2 = {{square()}}$$</p>
<button @click="addOne">Add One</button>
<button @click="randomInt">Random Number</button>
</div>
</template>
<script>
import math from 'mathjs'
import MathLive from 'mathlive'
export default {
name: 'Problem',
data: function () {
return {
num: math.randomInt(1,35)
}
},
watch: {
num: function () {
console.log("Data changed");
// this.renderMath();
}
},
created: function () {
console.log("Hello This is created!");
this.renderMath();
},
beforeMount: function () {
console.log("This is beforeMount");
},
mounted: function () {
console.log("This is mounted!");
},
beforeUpdate: function () {
console.log("This is beforeUpdate");
this.renderMath();
},
methods: {
addOne: function() {
this.num++
},
randomInt: function () {
this.num = math.randomInt(1,35)
},
square: function () {
return this.num**2
},
renderMath: function (event) {
this.$nextTick(function(){
MathLive.renderMathInElement("math");
})
}
}
}
</script>
<style lang="css" scoped>
@import url("../../node_modules/mathlive/dist/mathlive.core.css");
@import url("../../node_modules/mathlive/dist/mathlive.css");
p {
color: white;
}
</style>
编辑:为了澄清当我加载页面时,初始值是使用 MathLive 正确排版的,如下所示:
然后在我单击Add One或Random Number按钮后,程序应该生成一个新值,计算它的平方,然后更新屏幕上的该值如下所示: