Ionic 2 - ng2-chartjs2 working on browser but not in device

让人想犯罪 __ 提交于 2019-12-11 08:14:52

问题


I have managed to implement ng2-chartjs2 in my ionic2 application. It is perfectly working when i run it on browser (i.e ionic serve or ionic run android -l -c) but when i tried it on device (i.e ionic run android), it simply shows a blank page.

I have uploaded the working sample project in my repo here

Any help would be appreciated. Thanks.

Still expecting someone to clear this issue.


回答1:


Well the issue here is that the Chart.bundle.js isn't on the expected path on android build.

Add this:

var availablePlatforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);

var filestocopy = [
  {"src/assets/libs/Chart.bundle.js": "platforms/android/assets/libs/Chart.bundle.js"}
];

for(var x=0; x<availablePlatforms.length; x++) {

  var currentPlatform = availablePlatforms[x].trim().toLowerCase();

  if (currentPlatform == 'android') {
    filestocopy.forEach(function(obj) {
      Object.keys(obj).forEach(function(key) {
        var val = obj[key];
        var srcfile = path.join(rootdir, key);
        var destfile = path.join(rootdir, val);
        var destdir = path.dirname(destfile);
        if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
          fs.createReadStream(srcfile).pipe(
              fs.createWriteStream(destfile));
        }
      });
    });

  }
}

At the end of your hooks -> after_prepare -> 010_add_platform_class.js, then change the script reference inside of your index.html to <script src="assets/libs/Chart.bundle.js"></script> (note: changed chart for Chart).

Good luck!



来源:https://stackoverflow.com/questions/40150362/ionic-2-ng2-chartjs2-working-on-browser-but-not-in-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!