我一直在努力使用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正确设置了初始值,如下所示: 然后,在单击添加一个或随机数按钮后,程序应生成一个新值,计算其平方,并在屏幕上更新该值,如下所示:
答案 0 :(得分:2)
MathLive的DOM操作似乎与Vue的虚拟DOM冲突,从而阻止Vue用更新的文本节点修补DOM。
一种解决方法是应用key
,以在p
更改时强制重新创建MathLive key
元素。我们可以使用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>
答案 1 :(得分:1)
您需要使用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>