未捕获到的ReferenceError:在Power BI Embedded中未定义$

时间:2018-11-14 16:43:29

标签: javascript asp.net-core-mvc asp.net-core-2.0 powerbi-embedded

我试图将Power BI仪表板嵌入我的ASP.NET Core 2.1应用程序中。运行此应用程序时出现JavaScript错误,提示$未定义。我已经在div中定义了此变量,并且试图在脚本中访问它。这是我的index.cshtml代码。

@model WebApplication2.Models.EmbedConfig
    @{
        ViewBag.Title = "Dashboard";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <script src="https://npmcdn.com/es6-promise@3.2.1"></script>
    <script src="~/js/powerbi.js"></script>
    @if (!string.IsNullOrEmpty(Model.ErrorMessage))
    {
        <div id="errorWrapper">
            <h2>
                Error
            </h2>
            @Model.ErrorMessage
        </div>

        return;
    }
    <br />

    <a href="javascript:void(0);" class="btn btn-primary" id="backLink">Back</a>
    <br /><br />

    <h2>Embedded Dashboard</h2>

    <br />

    <div id="dashboardContainer"></div>

    <script>

        var accessToken = "@Model.EmbedToken.Token";

        var embedUrl = "@Html.Raw(Model.EmbedUrl)";

        var embedDashboardId = "@Model.Id";

        var models = window['powerbi-client'].models;

        var config = {
            type: 'dashboard',
            tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedDashboardId,

            settings: {
                background: models.BackgroundType.Transparent
            }
        };

        // Get a reference to the embedded dashboard HTML element
        var dashboardContainer = $('#dashboardContainer')[0];

        // Embed the dashboard and display it within the div container.
        var dashboard = powerbi.embed(dashboardContainer, config);
    </script>

这是我的.net core 2.1应用程序中的Index.cshtml。我收到一个错误消息,说$未定义。我已经明确定义了dashboardContainer。为什么会出现此错误?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

似乎您需要包含jquery。尝试使用经典javascript代替,看看是否是问题所在。

替换 var consoleContainer = $('#dashboardContainer')[0]; 与 vardashboardContainer = document.getElementById(“ dashboardContainer”);