PowerBi使用JavaScript嵌入仪表板

PowerBi使用JavaScript嵌入仪表板,javascript,powerbi,powerbi-embedded,Javascript,Powerbi,Powerbi Embedded,我在PowerBi中创建了一个仪表板,可以使用示例项目加载到.cs页面中,但我喜欢尝试使用JavaScript API。我尝试使用project[GitHub示例项目,但在模型方面出现了错误。我会使用其他功能吗?我相信我已经安装了所有的js库,但仪表板不会加载到我的html页面中 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></titl

我在PowerBi中创建了一个仪表板,可以使用示例项目加载到.cs页面中,但我喜欢尝试使用JavaScript API。我尝试使用project[GitHub示例项目,但在模型方面出现了错误。我会使用其他功能吗?我相信我已经安装了所有的js库,但仪表板不会加载到我的html页面中

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script>
    <script src="scripts/step_interact.js"></script>
    <script src="scripts/step_embed.js"></script>
    <script src="scripts/step_authorize.js"></script>
    <script src="/bower_components/powerbi-client/dist/powerbi.js"></script>
    <script>

    $(document).ready(function () {

        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;

        var embedConfiguration = {
            type: 'dashboard',
            id: 'dashboardID',
            embedUrl: 'https://app.powerbi.com/reportEmbed',
            tokenType: models.TokenType.Aad,
            accessToken: 'TokenString'
        };

        var $dashboardContainer = $('#embedContainer');
        var dashboard = powerbi.embed($dashboardContainer.get(0), embedConfiguration);
    });

    </script>
    </head >
            <body>
                <div id="embedContainer"></div>         
</body >
</html >

$(文档).ready(函数(){
//获取模型。模型包含可以使用的枚举。
var模型=窗口['powerbi-client']。模型;
变量配置={
键入:“仪表板”,
id:'仪表板id',
嵌入URL:'https://app.powerbi.com/reportEmbed',
tokenType:models.tokenType.Aad,
accessToken:'TokenString'
};
变量$dashboardContainer=$(“#EmbeddeContainer”);
var dashboard=powerbi.embed($dashboardContainer.get(0),embedConfiguration);
});

您可以尝试使用Javascript显示PowerBI仪表板的代码。您只需要有效的访问令牌和仪表板ID


window.onload=函数(){
var模型=窗口['powerbi-client']。模型;
变量配置={
键入:“仪表板”,
accessToken:{{access token}},
嵌入URL:'https://app.powerbi.com/dashboardEmbed?dashboardId={{dashboard id}]
};  
var$reportContainer=$(“#dashboardContainer”);
var report=powerbi.embed($reportContainer.get(0),embedConfiguration);
}
有几件事:

  • 通过查看您引用的powerbi.js文件,尝试更改以下行:
    var models=window['powerbi-client']。models
    var models=window.powerbi.models

  • 请确保使用正确的embedURL,当您应该使用/dashboardEmbed时,您正在使用/reportEmbed

  • 最终,您的代码应该如下所示:

    $(文档).ready(函数(){
    //获取模型。模型包含可以使用的枚举。
    var models=powerbi.models;//或window.powerbi.models
    变量配置={
    键入:“仪表板”,
    id:'仪表板id',
    嵌入URL:'https://app.powerbi.com/dashboardEmbed',
    tokenType:models.tokenType.Aad,
    accessToken:'TokenString'
    };
    变量$dashboardContainer=$(“#EmbeddeContainer”);
    var dashboard=powerbi.embed($dashboardContainer.get(0),embedConfiguration);
    
    });
    您需要将模型初始化更改为:

    import * as pbi from 'powerbi-client'
    
    
    const models = pbi.models
    
    不要使用此示例:

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

    确保您已在“Power BI应用程序注册工具”中将准确的URL注册为重定向URL。下面的代码在设置正确的重定向URL后可以正常工作

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

    错误是什么?无法获取未定义或空引用的属性“models”我猜您可能在演示项目中获得了此代码,但是,在Power BI社区中,这看起来像是地址问题,演示代码可能试图访问Microsoft内部地址,我建议您尝试下载Git代码并像git页面所说的那样使用,而不是像演示所做的那样使用。看这篇参考资料,一个家伙和你有相同的问题:Hi@Yash-我尝试了你的代码,但我仍然收到一个关于未定义模型的错误。JavaScript/master/dist/powerbi.js由于mime类型不匹配dashboard.html SCRIPT5007:无法获取pro未定义或空引用dashboard.html的perty“models”(13,9)