从 typescript 引用 .cshtml 中的全局变量集

Reference a global var set in .cshtml from typescript

我在我的 .cshtml 文件中设置了一些变量,以便在我的 angular 服务中使用它们:

<script>
        var _APIURL = '@(Url.Content("~/Admin/api/"))';
        var _AREAURL = '@(Url.Content("~/Admin/"))';
        var _APPURL = '@(Url.Content("~/"))';
        var _AREAPATH = "@(Url.Content("~/") + "Areas/" + HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"])";
</script>  

然后,我想在我的模块中引用它们,类似于:

((): void => {
    var app = angular.module('system', ['ngRoute', 'ui.bootstrap', 'gettext', 'ngCookies', 'ipCookie']);


    app.constant("_AREAPATH", _AREAPATH); // this value comes from the view
    app.constant("_AREAURL", _AREAURL); // this value comes from the view
    app.constant("_APPURL",  _APPURL); // this value comes from the view

    app.config(["$routeProvider", "_AREAPATH", ($routeProvider, areaPath) => {
        $routeProvider
            .when('/', { templateUrl: areaPath + "/Templates/System/System.html" })
            .otherwise({
                template: "This doesn't exist!"
            });
    }]);
})()

但是我收到 "Cannot resolve symbol _AREAURL" 错误。我怎样才能使这项工作?

我认为您需要在 TypeScript 中添加一些环境定义:

declare var _APIURL: string;
declare var _AREAURL: string;
declare var _APPURL: string;
declare var _AREAPATH: string;