【问题标题】:Why is there an error in ChartJs.Blazor (change in ASPNETCORE_ENVIRONMENT)为什么 ChartJs.Blazor 有错误(更改 ASPNETCORE_ENVIRONMENT)
【发布时间】:2021-03-19 05:28:06
【问题描述】:

我已经安装了 NugetPacket:ChartJs.Blazor (v1.1.0)。在 _Host.cshtml 文件中添加了

 <script src="_content/ChartJs.Blazor/moment-with-locales.min.js" type="text/javascript" language="javascript"></script>

<!-- Reference the included ChartJs javascript file. -->
<script src="_content/ChartJs.Blazor/Chart.min.js" type="text/javascript" language="javascript"></script>

<!-- This is the glue between the C# code and the ChartJs charts -->
<script src="_content/ChartJs.Blazor/ChartJsBlazorInterop.js" type="text/javascript" language="javascript"></script>

<!-- Some styling -->
<link rel="stylesheet" href="_content/ChartJs.Blazor/ChartJSBlazor.css" />

当 ASPNETCORE_ENVIRONMENT 设置为 Development 时,它可以工作,但是当 ASPNETCORE_ENVIRONMENT 更改为 Production 时,应用程序崩溃并返回错误:

Microsoft.JSInterop.JSException: Could not find 'ChartJsInterop' in 'window'.
Error: Could not find 'ChartJsInterop' in 'window'.

为什么在生产模式中出现错误,而不是在开发模式中?这个问题有什么解决办法吗?

【问题讨论】:

    标签: c# chart.js blazor


    【解决方案1】:

    您确定没有将它添加到&lt;environment&gt; HTML 标记中吗?示例:

    <environment exclude="Production">
       <script src="_content/ChartJs.Blazor/ChartJsBlazorInterop.js" type="text/javascript" language="javascript"></script>
    </environment>
    

    参考:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/environment-tag-helper?view=aspnetcore-5.0

    【讨论】:

    • 代码只放在 标签内。环境在 launchSettings.json "environmentVariables" 中定义。
    • 您能否使用浏览器中的开发人员工具验证该文件是否正在从服务器检索并呈现在已编译的 HTML 中?
    • 如果我进行检查 -> Sources 有一个“文件”_content/CharJs.Blazor,里面是 ChartJSBlazor.css。如果我点击 ChartJSBlazor.css,里面什么都没有(它是空的,没有内容)。可能是什么问题?
    • @kartaku 这听起来很傻,但右键单击解决方案并转到“清洁解决方案”。然后再试一次。似乎这些文件没有包含在输出目录中。
    • 我尝试了“清洁解决方案”、“重建解决方案”和“构建解决方案”,但没有奏效。
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2023-03-03
    相关资源
    最近更新 更多