[英]Using CDNJS with LibMan for Moment.JS
I'm trying to get into Asp.Net Core 2.2 MVC right now.
我现在正在尝试进入Asp.Net Core 2.2 MVC 。 I want to use the
moment.js library for some date calculations due to values from a DevExpress datetime picker.
由于来自 DevExpress 日期时间选择器的值,我想使用moment.js库进行一些日期计算。 I have been able to include it according to the following instructions using "libman.json":
我已经能够根据以下说明使用“libman.json”包含它:
{
"provider": "cdnjs",
"library": "moment.js@2.23.0",
"destination": "wwwroot/lib/moment.js/",
"files": [
"moment-with-locales.min.js",
"locale/de.js"
]
}
The *.js files are available. *.js 文件可用。
However, the type or the factory 'moment()' cannot be used as described on the page https://momentjs.com/docs/ (there's no special sample for MVC either).但是,不能按照页面https://momentjs.com/docs/ 中的描述使用类型或工厂“moment()”(也没有针对 MVC 的特殊示例)。 It does not exist.
它不存在。 I have also tried to use the nuget 'Moment.js', but it is also not possible to reference it.
我也尝试使用 nuget 'Moment.js',但也无法引用它。
I tried something like this in my *.cshtml ... but that's more Angular rather than MVC, isn't it?我在我的 *.cshtml 中尝试过类似的东西……但这更像是 Angular 而不是 MVC,不是吗?
@section Scripts { @section 脚本 {
<script>
import $ from jquery;
import moment from moment;
const m = moment();
function dateDiff(secondDate) {
//var a = moment([2008, 9]);
//var b = moment([2007, 0]);
//a.diff(b, 'years');
return $("#age").text("" + " years");
}
</script>
} }
I'm not able to get access to the moment.js type.我无法访问 moment.js 类型。 What do I have to do?
我需要做什么? What have I done wrong?
我做错了什么?
Thanks in advance!提前致谢! Kind regards.
亲切的问候。
Before using moment
, you need to reference it.在使用
moment
之前,您需要引用它。
For short using, you could try like below:对于简短的使用,您可以尝试如下:
@section Scripts{
<script src="~/lib/moment.js/moment-with-locales.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var now = moment();
alert(now);
var a = moment([2008, 9]);
var b = moment([2007, 0]);
alert(a.diff(b, 'years'));
});
</script>
}
You also could register moment
in global by _Layout.cshtml
.您也可以通过
_Layout.cshtml
在 global 中注册moment
。
<!DOCTYPE html>
<html>
//rest code
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
</environment>
<environment exclude="Development">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="sha256-E/V4cWE4qvAeO5MOhjtGtqDzPndRO1LBk8lJ/PR7CA4=">
</script>
</environment>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/lib/moment.js/moment-with-locales.min.js"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
@Tao Zhou - thank you very much for your answer. @Tao Zhou - 非常感谢您的回答。 Of course I tried to reference/register it in my _layout.cshtml.
当然,我尝试在我的 _layout.cshtml 中引用/注册它。
The problem was somewhere else... I have tested CodeRush from DevExpress as an IntelliSense...... For JavaScript this IntelliSense does not seem to recognize the type for moment.js, and maybe any type of js.问题出在其他地方……我已经将 DevExpress 的 CodeRush 作为 IntelliSense 进行了测试……对于 JavaScript,此 IntelliSense 似乎无法识别 moment.js 的类型,也可能无法识别任何类型的 js。 So I thought the reference would not work, but in fact it was just a problem with the IDE extension!
所以我以为引用不行,其实只是IDE扩展的问题!
So ReSharper... here we are again :)所以 ReSharper ......我们又来了 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.