從ASP.NET MVC視圖調用TypeScript類方法

[英]Calling a TypeScript class method from a ASP.NET MVC view


I have a TypeScript class like the following

我有一個類似如下的TypeScript類

export class A
{
     myFunc(n : number){
         alert(n);
     }
}

I need to render an attribute on my select element onchange event so it will call the TypeScript class method.

我需要在我的select元素onchange事件上呈現一個屬性,因此它將調用TypeScript類方法。

writer.AddAttribute(HtmlTextWriterAttribute.Onchange,
           string.Format("javascript:new A().myFunc({0})", this.Number));

My question is how can I find the exact definition so I will be able to render a proper call to the TypeScript function?

我的問題是如何找到確切的定義,以便我能夠正確調用TypeScript函數?

This is how the compiled js looks like...

這就是編譯后的js的樣子......

define("app", ["require", "exports", "jquery"], function (require, exports, $) {
   "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });

    function myFunc(rsw_id) {
       alert(rsw_id);
    }
});

1 个解决方案

#1


0  

I hope it will help someone.
When using export and import for classes etc. the generated JavaScript code is dependent upon requireJS and you need to properly install it in your project and configure it.
RequireJS is great but in some cases you just separate your classes to different files and not into different modules.
Combining RequireJS with ASP.NET MVC is possible and there are great articles out there but for me it was an overkill and complicated the code without real neccassity. In my application there is only one module.
So, I removed all the export/import expressions from the classes and selected None for module system setting (At the project properties, TypeScript section). Now the generated javascript exposes all the classes as functions that can be easily addressed.

我希望它會幫助某人。對類等使用導出和導入時,生成的JavaScript代碼依賴於requireJS,您需要在項目中正確安裝並配置它。 RequireJS很棒,但在某些情況下,您只需將類分隔為不同的文件而不分成不同的模塊。將RequireJS與ASP.NET MVC結合起來是可能的,並且有很多很棒的文章,但對我來說這是一個過度的,並且沒有真正的neccassity復雜的代碼。在我的應用程序中,只有一個模塊。因此,我從類中刪除了所有導出/導入表達式,並為模塊系統設置選擇了None(在項目屬性,TypeScript部分)。現在生成的javascript將所有類公開為可以輕松解決的函數。

This article really helped me: http://www.mobzystems.com/blog/creating-a-simple-typescript-web-app/

這篇文章對我有幫助:http://www.mobzystems.com/blog/creating-a-simple-typescript-web-app/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/11/27/1caf428faa21c6b429ccf5f9863d4c0f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com