简体   繁体   English

在 ionic 项目中包含 3rd 方模块的正确方法

[英]The proper way to include 3rd party module in ionic project

So, I'm interested to use ionic-audio by arielfaur所以,我有兴趣使用arielfaur 的 ionic-audio

into my ionic project.进入我的离子项目。

My question I guess in general is what's the proper way to do this.我想我的问题一般是这样做的正确方法是什么。 If I download the zip file from github, where should I put this extracted files as part of ionic project, that way if there's update to the module, I can just download the zip file and extract to the same directory without worrying about different files in different folder.如果我从 github 下载 zip 文件,我应该把这个提取的文件作为 ionic 项目的一部分放在哪里,这样如果模块有更新,我就可以下载 zip 文件并解压到同一目录,而不必担心不同的文件不同的文件夹。

What's the best practice in here?这里的最佳做法是什么?

Use the suggested directions in the Docs , this is the most common way to include 3rd Party Plugins not found on ngCordova since Ionic makes use of Bower.使用Docs 中的建议说明,这是包含ngCordova上未找到的 3rd Party Plugins 的最常用方法,因为 Ionic 使用了 Bower。 By using Bower, you don't need to go to the actual Github repo, download the source and have to worry about updates in the future.通过使用 Bower,您无需前往实际的 Github 存储库,下载源代码,也不必担心未来的更新。 Just simply do bower install --save <pkg> where <pkg> is the name of the 3rd party module you'd like to install.只需简单地执行bower install --save <pkg>其中<pkg>是您要安装的第 3 方模块的名称。

If you used ionic start , when doing a bower install --save <pkg> , it will install your dependencies into your www/lib directory.如果您使用ionic start ,则在执行bower install --save <pkg> ,它会将您的依赖项安装到您的www/lib目录中。 This is because ionic will add a .bowerrc file that changes your bower install folder from ./bower_components to whatever is specified in the .bowerrc file's directory attribute.这是因为 ionic 将添加一个 .bowerrc 文件,该文件将您的 bower 安装文件夹从./bower_components更改为 .bowerrc 文件的directory属性中指定的任何内容。

.bowerrc file .bowerrc 文件

{
  "directory": "www/lib"
}

Usage用法

Install dependencies安装依赖

Cordova media plugin Cordova 媒体插件

ionic plugin add cordova-plugin-media

Install this module using bower使用 bower 安装此模块

bower install ionic-audio

There's a sample Ionic project in the folder example-audio.文件夹 example-audio 中有一个示例 Ionic 项目。 The project contains not platforms, so you must add one and make a build if you want to test it on your device.该项目不包含平台,因此如果您想在您的设备上测试它,您必须添加一个并进行构建。 Keep in mind that the module depends on a Cordova plugin so the module won't run locally with ionic serve.请记住,该模块依赖于 Cordova 插件,因此该模块不会在本地使用 ionic serve 运行。 However, you can still run the project locally to tune the UI before deploying to the device.但是,您仍然可以在本地运行项目以在部署到设备之前调整 UI。

Include JS file包含JS文件

<script src="dist/ion-audio.js"></script>

Inject the dependency in your app's module在应用程序的模块中注入依赖项

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ionic-audio'])

I've been working with Ionic for a while now, so what I would recommend you is to Implement the track list in the specific controller you need the music player in我已经与 Ionic 合作了一段时间,所以我建议您在需要音乐播放器的特定控制器中实现曲目列表

$scope.tracks = [
    {
        url: 'https://ionic-audio.s3.amazonaws.com/Message%20in%20a%20bottle.mp3',
        artist: 'The Police',
        title: 'Message in a bottle',
        art: 'https://ionic-audio.s3.amazonaws.com/The_Police_Greatest_Hits.jpg'
    },
    {
        url: 'https://ionic-audio.s3.amazonaws.com/Roxane.mp3',
        artist: 'The Police',
        title: 'Roxane',
        art: 'https://ionic-audio.s3.amazonaws.com/The_Police_Greatest_Hits.jpg'
    }
];

And since Ionic has it's own directives, you only need to implement the ion-audio-track in the view you need.由于 Ionic 有它自己的指令,你只需要在你需要的视图中实现ion-audio-track This is what I personally prefer if the music player will be in a single view.如果音乐播放器处于单一视图中,这是我个人更喜欢的。

      <ion-audio-track ng-repeat="track in tracks" track="track">
    <div class="card">
      <div class="item item-thumbnail-left">
        <img src="{{track.art}}">

        <h2>{{track.title}}</h2>

        <p>{{track.artist}}</p>
        <ion-audio-controls>
          <a class="button button-icon icon" ion-audio-play></a>
          <ion-spinner icon="ios" style="position: relative; top: 8px; left: 4px"></ion-spinner>
        </ion-audio-controls>
      </div>
      <div class="item item-divider">
        <ion-audio-progress-bar display-time></ion-audio-progress-bar>
      </div>
    </div>
  </ion-audio-track>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM