【问题标题】:MathJax is not defined angular 4MathJax 未定义角度 4
【发布时间】:2018-11-10 01:07:06
【问题描述】:

我已经运行命令 npm install mathjax 和 npm install --save @types/mathjax

并在 angular-cli.json 中添加了 mathjax.js 的引用

"../node_modules/mathjax/mathjax.js?config=TeX-AMS-MML_HTMLorMML"

。我已经创建了指令

import { Directive, Input, OnChanges, ElementRef } from '@angular/core';
    declare var MathJax: any;

    @Directive({
    selector: '[MathJax]'
    })
    export class MathJaxDirective implements OnChanges {

        @Input('MathJax') private value: string;

        constructor(private element: ElementRef) {

    }

    ngOnChanges() {

        // console.log('- onChange -');
        // console.log(this.value);

        this.element.nativeElement.innerHTML = this.value;
        MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.element.nativeElement]);
        }

    }

这显示错误 Mathjax ReferenceError: MathJax is not defined

我在 tsconfig.app.json 类型中添加了 ma​​thjax

        {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "outDir": "../out-tsc/app",
        "baseUrl": "./",
        "module": "es2015",
        "types": [
          "mathjax"
        ]
      },
      "exclude": [
        "test.ts",
        "**/*.spec.ts"
      ]
    }       

【问题讨论】:

    标签: angular mathjax


    【解决方案1】:

    你得到 MathJax 'undefined' 因为它没有加载到你的浏览器。 要么添加

     <script type="text/javascript" async
      src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML">
    </script>
    

    或者替换

    declare var MathJax: any;
    

    与

    import { MathJax } from 'mathjax';
    

    并将“mathjax”添加到 src/tsconfig.app.json 中的 types 数组中。

    【讨论】:

    • 我尝试使用 import {MathJax } from 'mathjax' 显示错误 -e:/...//node_modules/@types/mathjax/index.d.ts' is not a module跨度>
    • 你是否在 src/tsconfig.app.json 的 types 数组中添加了“mathjax”?
    • 我使用了 import 'mathjax' 。错误消失了,但仍然无法正常工作
    • 嗨,我也面临同样的问题。你解决了吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 2014-03-07
    • 2021-04-20
    • 2015-05-22
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多