使用RequireJS .net core / Razor加载节点模块

时间:2019-03-07 18:05:37

标签: typescript asp.net-core .net-core requirejs node-modules

我知道可以使用requirejs加载节点模块,并且我已经按照其页面上的指南进行了尝试,但我尝试了几件事,但是又出现了同样的错误!如果我使用了他们说的“ r.js”文件,似乎baseUrl会发生变化,但是当我添加该文件时,它将更改我的baseUrl设置,并且无论我如何对其进行优先排序都始终会更改。

有没有办法使这项工作有效?我希望能够像往常一样在TS文件中使用导入功能,而不必将源文件移动到本地src文件夹中,以便requirejs可以找到该库。

_Layout.cshtml

<script src="~/js/_require.config.js"></script>
<script>require.baseUrl = '@Url.Content("~/js/")'</script>
<script src="~/lib/require.js"></script>
@RenderSection("Scripts", required: false)

_require_config.js

var require = {
	paths: {
		"jquery": "../../lib/jquery/dist/jquery",
		"bootstrap": "../../lib/bootstrap/dist/js/bootstrap",
		"knockout": "../../lib/knockout-3.4.2",
		"komapping": "../../lib/knockout.mapping-latest.debug",
		"knockout.validation": 'knockout.validation.min',
		"datatables": "../../adminLTE/plugins/datatables/jquery.dataTables",
		"select2": "select2.min",
		"lodash": "lodash.min",
		"sweetalert2": "sweetalert2.min",
		"tslib": "../../lib/tslib/tslib"
	},
	deps: ['knockout', 'komapping'],
	callback: function (ko, mapping) {
		ko.mapping = mapping;
	}
};

if (typeof exports === 'object') {
	exports.config = require;
}

TSconfig

{
	"compilerOptions": {
		"module": "amd",
		"moduleResolution": "node",
		"noImplicitAny": false,
		"noFallthroughCasesInSwitch": true,
		"noImplicitThis": true,
		"noUnusedLocals": false,
		"experimentalDecorators": true,
		"noUnusedParameters": false,
		"importHelpers": true,
		"outDir": "./wwwroot/js",
		"lib": [
			"dom",
			"es5",
			"es2015.promise",
			"es2018.promise",
			"esnext.asynciterable"
		],
		"typeRoots": [
			"./node_modules/@types"
		]
	},
	"include": [
		"./wwwroot/ts/**/*",
		"./wwwroot/lib/tslib/**/*",
		"./custom_types/**/*"
	],
	"exclude": [
		"./node_modules",
		"./bower_components"
	]
}

编译的JS

define(["require", "exports", "knockout", "../init", "../Shared/generic-sweetalert", "../Shared/default-ajax-requests", "../Models/models", "../Shared/loader", "class-validator"], function (require, exports, ko, init_1, generic_sweetalert_1, default_ajax_requests_1, models_1, loader_1, class_validator_1) {
    "use strict";
    exports.__esModule = true;
    function init(pingUrl, pingUrlForEquimentHtml, pingUrlForEquimentJson) {
        init_1.onready(function () {
            var dataGrab = default_ajax_requests_1.DefaultAjaxGetRequest(pingUrl);
            dataGrab.then(function (val) {
                if (val.code == models_1.StatusCodes.Ok) {
                    loader_1.ToggleLoadingAnimation();
                    ko.applyBindings(new editOrder(val.data));
                }
                else {
                    generic_sweetalert_1.BackendSweetAlert(val.data.responseJSON.sweetAlert);
                }
            });
        });
    }
    exports.init = init;
    var editOrder = /** @class */ (function () {
        function editOrder(serverModel) {
            var _this = this;
            this.model = ko.mapping.fromJS(serverModel);
            this.localUrl = document.URL;
            this.saveOrder = function () {
                class_validator_1.validate(_this.model.Data).then(function (errors) {
                    console.log(errors);
                });
            };
        }
        return editOrder;
    }());
});

0 个答案:

没有答案