使用 MathLive 和 Vue 更新数学方程式
Update math equation using MathLive and Vue
我一直在努力使用 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 按钮后,程序应生成一个新值,计算其平方,并更新该值画面如下图:
您需要使用 vue.js
computed 属性
new Vue({
name: 'Problem',
data: function () {
return {
num: math.randomInt(1,35)
}
},
watch: {
num: function () {
console.log("Data changed");
this.renderMath();
}
},
computed: {
square: function () {
return this.num**2;
}
},
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)
},
renderMath: function (event) {
this.$nextTick(function(){
MathLive.renderMathInElement("math");
})
}
}
}).$mount("#app")
<script src="https://unpkg.com/mathjs/dist/math.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mathlive@0.26.0/dist/mathlive.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<span>$${{num}}^2 = {{square}}$$</span>
<span id="math"></span>
<button @click="addOne">Add One</button>
<button @click="randomInt">Random Number</button>
</div>
似乎 MathLive 的 DOM 操作与 Vue 的虚拟 DOM 冲突,阻止 Vue 使用更新的文本节点修补 DOM。
解决方法是应用 key
以在 key
更改时强制重新创建 MathLive p
元素。我们可以使用 num
作为键,因为它会随着每次按下按钮而改变:
<p :key="num">...</p>
num
上的当前观察者需要更新以调用 renderMath()
来刷新 MathLive 元素:
watch: {
num() {
this.renderMath();
}
},
您还应该考虑使 square()
成为 more efficient rendering 的计算 属性:
// script
computed: {
square() {
return this.num ** 2
}
}
// template
<p :key="num">$${{num}}^2 = {{square}}$$</p>
我一直在努力使用 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 正确排版,如下所示:
您需要使用 vue.js
computed 属性
new Vue({
name: 'Problem',
data: function () {
return {
num: math.randomInt(1,35)
}
},
watch: {
num: function () {
console.log("Data changed");
this.renderMath();
}
},
computed: {
square: function () {
return this.num**2;
}
},
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)
},
renderMath: function (event) {
this.$nextTick(function(){
MathLive.renderMathInElement("math");
})
}
}
}).$mount("#app")
<script src="https://unpkg.com/mathjs/dist/math.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mathlive@0.26.0/dist/mathlive.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<span>$${{num}}^2 = {{square}}$$</span>
<span id="math"></span>
<button @click="addOne">Add One</button>
<button @click="randomInt">Random Number</button>
</div>
似乎 MathLive 的 DOM 操作与 Vue 的虚拟 DOM 冲突,阻止 Vue 使用更新的文本节点修补 DOM。
解决方法是应用 key
以在 key
更改时强制重新创建 MathLive p
元素。我们可以使用 num
作为键,因为它会随着每次按下按钮而改变:
<p :key="num">...</p>
num
上的当前观察者需要更新以调用 renderMath()
来刷新 MathLive 元素:
watch: {
num() {
this.renderMath();
}
},
您还应该考虑使 square()
成为 more efficient rendering 的计算 属性:
// script
computed: {
square() {
return this.num ** 2
}
}
// template
<p :key="num">$${{num}}^2 = {{square}}$$</p>