简体   繁体   English

将MVC模型数据传递到客户端TypeScript代码

[英]Passing MVC Model Data to Client-side TypeScript Code

When using MVC, I sometimes pass the server's model data to the client-side JavaScript by using Razor injected into the JavaScript, as follows: 使用MVC时,有时我会通过使用注入到JavaScript中的Razor将服务器的模型数据传递给客户端JavaScript,如下所示:

<script type="text/javascript">
    var myClientGuid = '@Model.MyServerGuid';
</script>

This sets a JavaScript variable named myClientGuid to the value of the server-side model property MyServerGuid . myClientGuid名为myClientGuid的JavaScript变量设置为服务器端模型属性MyServerGuid When it reaches the client, the code looks something like this inside the browser: 当到达客户端时,代码在浏览器中看起来像这样:

<script type="text/javascript">
    var myClientGuid = 'EF0077AB-0482-4D91-90A7-75285F01CA6F';
</script>

This allows external JavaScript files to use this variable. 这允许外部JavaScript文件使用此变量。

My question is, in TypeScript, since all code must be referenced via external files, what is the best way to pass server-side fields to TypeScript code? 我的问题是,在TypeScript中,由于必须通过外部文件引用所有代码,因此将服务器端字段传递给TypeScript代码的最佳方法是什么? External code files cannot contain Razor code. 外部代码文件不能包含Razor代码。 Should I use the same technique as above, in the View, mixing JavaScript and Typescript within the project? 我是否应该在视图中使用与上述相同的技术,在项目中混合使用JavaScript和Typescript?

The TypeScript compiler just needs to know that your server-side fields exist. TypeScript编译器只需要知道您的服务器端字段存在。 The easiest way to do that is to use ambient declarations (see section 10 of the spec). 最简单的方法是使用环境声明(请参阅规范的第10节)。 For example, if you had a .ts file that needed to use myClientGuid, you could do 例如,如果您有一个需要使用myClientGuid的.ts文件,则可以执行

declare var myClientGuid: string;

at the top of your main .ts file. 在主要.ts文件的顶部。 The compiler will not generate code for this var declaration, so you won't clobber anything. 编译器不会为此var声明生成代码,因此您不会破坏任何内容。 Now any files that reference that .ts file will know that there is a myClientGuid string available in global scope. 现在,所有引用该.ts文件的文件都将知道在全局范围内有一个myClientGuid字符串可用。

Another solution (to avoid global variables) is to wrap the TypeScript code in a function that takes the needed server-side fields as parameters: 另一个解决方案(避免全局变量)是将TypeScript代码包装在将所需服务器端字段作为参数的函数中:

In TypeScript file: 在TypeScript文件中:

function setupMyPage(myGuid:string) {
   ...
}

In .cshtml: 在.cshtml中:

<script src='@Url.Content("<path-to-typescript>")'></script>
<script>
    setupMyPage('@Model.MyServerGuid');
</script>

If you are using RequireJS, you can also export the setupMyPage function as a module, to avoid adding the function to global namespace: 如果使用的是RequireJS,则还可以将setupMyPage函数导出为模块,以避免将函数添加到全局名称空间:

In TypeScript file: 在TypeScript文件中:

export = setupMyPage;

In .cshtml: 在.cshtml中:

<script>
    require(['@Url.Content("<path-to-typescript>")'], function(setupMyPage) {
        setupMyPage('@Model.MyServerGuid');
    };
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM