uni-app MathJax用renderjs加载后,H5可以解析,APP端报错
uni-app MathJax用renderjs加载后,H5可以解析,APP端报错
产品分类
- uniapp/App
PC开发环境
操作系统 | 版本号 |
---|---|
Windows | 7 |
手机系统
系统 | 版本号 | 机型 |
---|---|---|
Android | Android 9.0 | 未知 |
页面类型
- vue
打包方式
- 云端
项目创建方式
- HBuilderX
示例代码:
<template>
<view class="content">
{{content}}
</view>
</template>
<script>
export default {
data() {
return {
content: '解:↵首先产生了中子、质子和电子,可知最先产生的元素为氢,其中子数比质子数多1的核素,其质量数为3,核素可表示为$_{1}^{3}\textrm{H}$或T,↵故答案为:氢;$_{1}^{3}\textrm{H}$或$T$↵'
};
},
methods: {
initMathJax() {
console.log(MathJax)
// TODO:公式解析
MathJax.Hub.Config({
"HTML-CSS": {
availableFonts: ["STIX-Web"],
preferredFont: "STIX-Web",
linebreaks: { automatic: false },
undefinedFamily: "STIXGeneral,'Arial Unicode MS',serif",
EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50),
webFont: "STIX-Web",
imageFont: "",
showMathMenu: false,
scale: 90
},
jax: ["input/TeX", "input/MathML", "output/HTML-CSS", "output/NativeMML", "output/CommonHTML"],
tex2jax: {
inlineMath: [
["$", "$"],
["\\(", "\\)"]
],
displayMath: [
["$$", "$$"],
["\\[", "\\]"]
],
processEscapes: true,
ignoreClass: "tex2jax_ignore|dno"
},
TeX: {
extensions: ["AMSmath.js", "AMSsymbols.js", "noErrors.js", "noUndefined.js"],
noUndefined: {
attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "100%" }
}
},
messageStyle: "none",
extensions: ["tex2jax.js", "mml2jax.js", "MathMenu.js", "MathZoom.js", "CHTML-preview.js"],
errorSettings: { message: ["parse..."] },
SVG: { linebreaks: { automatic: true }, width: "100% container" },
menuSettings: { CHTMLpreview: true }
})
MathJax.Hub.Queue(['Typeset', MathJax.Hub, ''])
}
}
}
</script>
<script module="MathJax" lang="renderjs">
export default {
mounted() {
if (typeof window.MathJax === 'object') {
this.initMathJax()
} else {
const script = document.createElement('script')
// script.src = 'https://www.stuspy.com/mathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
script.src = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
console.log('-----------')
console.log(this)
console.log('-----------')
script.onload = this.initMathJax.bind(this)
document.head.appendChild(script)
}
},
methods: {
// ...
}
}
</script>
<style lang="scss">
.content{font-size: 18px;margin-top: 150px;}
</style>
操作步骤:
- test.vue 文件放到项目中看效果
预期结果:
- test.vue 文件里的内容公式可以解析到
实际结果:
- test.vue 文件在APP端报错了,报错内容为TypeError: Cannot read property ‘bind’ of undefined at view.umd.min.js:1
bug描述:
- APP端报错 TypeError: Cannot read property ‘bind’ of undefined at view.umd.min.js:1,无法解析公式
- 用浏览器开发预览可以完成公式解析
更多关于uni-app MathJax用renderjs加载后,H5可以解析,APP端报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
4 回复
可以改用katex库使用看看。这个库渲染速度也比较高
更多关于uni-app MathJax用renderjs加载后,H5可以解析,APP端报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
意思是 initMathJax 不存在
App端渲染层和逻辑层并不在同一个js运行环境,你的 renderjs 里并没有这个方法
非常感谢,把initMathJax放到渲染层的methods后可以解析了
在uni-app中使用MathJax时,APP端报错的主要原因是renderjs的执行环境与H5不同。以下是关键点分析:
- 报错原因:
- APP端无法直接访问window对象,导致MathJax初始化失败
- renderjs在APP端运行在独立的JS引擎中,与H5环境有差异
- 解决方案: 修改script加载方式,确保在APP端也能正确初始化:
<script module="MathJax" lang="renderjs">
export default {
mounted() {
const init = () => {
if (typeof MathJax !== 'undefined') {
this.$ownerInstance.callMethod('initMathJax')
} else {
const script = document.createElement('script')
script.src = 'https://cdn.jsdelivr.net/npm/mathjax@2.7.9/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
script.onload = () => {
this.$ownerInstance.callMethod('initMathJax')
}
document.head.appendChild(script)
}
}
// 添加延迟确保环境就绪
setTimeout(init, 300)
}
}
</script>
- 其他注意事项:
- 使用https协议的CDN地址
- 通过$ownerInstance.callMethod调用vue方法
- 添加适当延迟确保环境初始化完成
- 兼容性处理: 建议对APP端和H5端做环境判断,使用条件编译:
// #ifdef H5
// H5专用代码
// #endif
// #ifdef APP
// APP专用代码
// #endif