qq_43063954 2022-10-29 11:09 采纳率: 44.4%
浏览 65
已结题

webPack5 TypeScript 的问题

我使用webPack5编写了一个简单的TypeScript项目 我的需求很简单就是在WPF中的浏览器调用JS里面的一部分代码

但我使用生产模式编译后就不能访问到我定义的方法或变量了。。所以我应该怎么做?


import * as monaco from 'monaco-editor';
import './index.css';
const axios = require('axios') ;

let editor= monaco.editor.create(document.body, {
    value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
    language: 'typescript',
    theme:'vs-dark'
});


//这是我要在.NET浏览器使用调用的方法 但使用生产模式编译后我无法访问到这个方法名了。。。
let GetText = function():string {
    return editor.getValue();
}
console.log(GetText());
运行结果及报错内容

img

img

需求

所以我如何才能在浏览器控制台调用 GetText() 方法

  • 写回答

2条回答 默认 最新

  • 游一游走一走 2022-10-29 11:21
    关注

    简单暴力,直接把他赋予给window对象

    
    import * as monaco from 'monaco-editor';
    import './index.css';
    const axios = require('axios') ;
    
    let editor= monaco.editor.create(document.body, {
        value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
        language: 'typescript',
        theme:'vs-dark'
    });
    
    
    //这是我要在.NET浏览器使用调用的方法 但使用生产模式编译后我无法访问到这个方法名了。。。
    let GetText = function():string {
        return editor.getValue();
    }
    window.GetText=GetText
    
    console.log(GetText());
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来