admin管理员组文章数量:1394518
I'm building a vue application and I want to use MathJax to render equations. I can get the equations to render when the ponent is mounted but I can't get the equations to rerender at a later point.
I made a code sandbox example to demonstrate this. Try to change the latex equation in the input and nothing changes even though the console shows the watcher is running.
Can someone please explain to me what I am doing wrong?
I added mathjax via cdn in the head of index.html with the following config
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'],['\\(','\\)']],
}
});
</script>
<script type="text/javascript" async src=".7.5/MathJax.js?config=TeX-AMS_CHTML"></script>
and here is the code in my app ponent
<template>
<div id="app">
<input v-model="latex"/><br>
<div>{{latex}}</div>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
latex: '$$\\frac{a}{b}$$'
}
},
methods: {
reRender() {
if(window.MathJax) {
console.log('rendering mathjax');
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub], () => console.log('done'));
}
}
},
mounted() {
this.reRender();
},
watch: {
latex: function() {
console.log('data changed')
this.reRender();
}
}
};
</script>
I'm building a vue application and I want to use MathJax to render equations. I can get the equations to render when the ponent is mounted but I can't get the equations to rerender at a later point.
I made a code sandbox example to demonstrate this. Try to change the latex equation in the input and nothing changes even though the console shows the watcher is running.
Can someone please explain to me what I am doing wrong?
I added mathjax via cdn in the head of index.html with the following config
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'],['\\(','\\)']],
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare./ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML"></script>
and here is the code in my app ponent
<template>
<div id="app">
<input v-model="latex"/><br>
<div>{{latex}}</div>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
latex: '$$\\frac{a}{b}$$'
}
},
methods: {
reRender() {
if(window.MathJax) {
console.log('rendering mathjax');
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub], () => console.log('done'));
}
}
},
mounted() {
this.reRender();
},
watch: {
latex: function() {
console.log('data changed')
this.reRender();
}
}
};
</script>
Share
Improve this question
asked Oct 3, 2018 at 22:29
Devin CrossmanDevin Crossman
7,60213 gold badges67 silver badges102 bronze badges
1 Answer
Reset to default 9I am able to make it work by adding 2 things.
https://codesandbox.io/s/x2mlq38m4z
add
key
to the div.key
is an indicator for virtual DOM. Whenkey
is the same, Vue may reuse the same div element. Whenkey
change, Vue will render a pletely new DOM element, which is helpful when you want to apply 3rd party library's code (MathJax) to the DOM from scratch.<div :key="latex">{{latex}}</div>
use $nextTick so that the rerender happens after the DOM have updated.
like this
watch: {
latex: function() {
console.log('data changed');
this.$nextTick().then(()=>{
this.reRender();
});
}
}
本文标签: javascriptmathjaxvue not rerendering equationsStack Overflow
版权声明:本文标题:javascript - mathjax + vue not rerendering equations - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744101033a2590870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论