【问题标题】:Use .js file in ionic 4在 ionic 4 中使用 .js 文件
【发布时间】:2019-08-21 01:57:30
【问题描述】:

我是 ionic 新手,我想在一页中使用 .js 文件

我有一个.js 文件,它是在画布中创建气泡,

我想做的是,想在我的 ionic 4 项目中使用那个 .js 文件并在我的主页上显示气泡。

这是我要使用的那个 codepen 的 Link

我在'assets/js/bubblefile.js' 中创建了文件,但我不知道如何在home.htmlhome.ts 中使用'bubblefile.js' 文件?下面是我的代码。

已编辑

home.html代码:

<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

  <script src="assets/js/bubblefile.js"></script>

</ion-content>

home.ts代码

import { Component } from '@angular/core';
import './assets/js/bubblefile';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

}

bubblefile.js 代码

var nodes = new vis.DataSet([
    {label: "Pop"},
    {label: "Alternative"},
    {label: "Rock"},
    {label: "Jazz"},
    {label: "Hits"},
    {label: "Dance"},
    {label: "Metal"},
    {label: "Experimental"},
    {label: "Rap"},
    {label: "Electronic"},
]);
var edges = new vis.DataSet();

var container = document.getElementById('bubbles');
var data = {
    nodes: nodes,
    edges: edges
};

var options = {
    nodes: {borderWidth:0,shape:"circle",color:{background:'#F92C55', highlight:{background:'#F92C55', border: '#F92C55'}},font:{color:'#fff'}},
    physics: {
        stabilization: false,
        minVelocity:  0.01,
        solver: "repulsion",
        repulsion: {
            nodeDistance: 40
        }
    }
};
var network = new vis.Network(container, data, options);


// Events
network.on("click", function(e) {
    if (e.nodes.length) {
        var node = nodes.get(e.nodes[0]);
        // Do something
        nodes.update(node);
    }
});
export { nodes, edges, container, data, options, network };

项目结构

任何帮助或建议将不胜感激,

谢谢

【问题讨论】:

    标签: javascript typescript ionic-framework ionic4


    【解决方案1】:

    如果你想在 HTML 文件中使用它:

    <script src="assets/js/bubblefile.js"></script>
    

    如果你想在 JavaScript/TypeScript 文件中使用它:

    将此添加到bubblefile.js 的底部:

    export { nodes, edges, container, data, options, network };
    

    在您要使用的文件的顶部:

    import "./assets/js/bubblefile";
    

    【讨论】:

    • 我需要在哪个文件顶部添加最后一行?
    • 在您的bubblefile.js 中添加export 行,并在您要使用bubblefile.js 的任何JS 文件中添加import 行。
    • 我想在.ts文件中使用它而不是在JS文件中
    • 感谢您的快速回放,但它给出了错误./src/app/home/home.page.ts Module not found: Error: Can't resolve './assets/js/bubblefile' in '/Users/atologistimac2/Documents/HybridApps/musicbubble/src/app/home'
    • 您的bubblefile.js 文件相对于您的home.page.ts 文件存储在哪里?
    【解决方案2】:

    home.html 代码:

        <ion-header>
          <ion-toolbar>
            <ion-title>
              Ionic Blank
            </ion-title>
          </ion-toolbar>
        </ion-header>
    
    <ion-content>
    
      <script src="assets/js/bubblefile.js"></script>
    
    </ion-content>
    

    home.ts 代码

    import { Component,OnInit } from '@angular/core';
    import * as bubble from './assets/js/bubble';
    declare var bubble: any;
    @Component({
      selector: 'app-home',
      templateUrl: 'home.page.html',
      styleUrls: ['home.page.scss'],
    })
    export class HomePage  implements OnInit{
    
        ngOninit(){
            bubble();
            }
    }
    

    bubble.js 代码

    var bubble = (function(){
        var nodes = new vis.DataSet([
            {label: "Pop"},
            {label: "Alternative"},
            {label: "Rock"},
            {label: "Jazz"},
            {label: "Hits"},
            {label: "Dance"},
            {label: "Metal"},
            {label: "Experimental"},
            {label: "Rap"},
            {label: "Electronic"},
        ]);
        var edges = new vis.DataSet();
    
        var container = document.getElementById('bubbles');
        var data = {
            nodes: nodes,
            edges: edges
        };
    
        var options = {
            nodes: {borderWidth:0,shape:"circle",color:{background:'#F92C55', highlight:{background:'#F92C55', border: '#F92C55'}},font:{color:'#fff'}},
            physics: {
                stabilization: false,
                minVelocity:  0.01,
                solver: "repulsion",
                repulsion: {
                    nodeDistance: 40
                }
            }
        };
        var network = new vis.Network(container, data, options);
    
    
        // Events
        network.on("click", function(e) {
            if (e.nodes.length) {
                var node = nodes.get(e.nodes[0]);
                // Do something
                nodes.update(node);
            }
        });
        export { nodes, edges, container, data, options, network };
    })
    

    【讨论】:

    • 检查你的文件路径
    【解决方案3】:

    有用于加载异步 JavaScript 文件的库。 https://www.npmjs.com/package/scriptjs

    安装包:

    npm i scriptjs
    

    然后在下面的任何地方使用它:

    import { get } from 'scriptjs';
    
    ngOnInit() {
    
        get("assets/js/searchEmp.js", () => {
            getSerchInspContext = this;
            loadSearchEmp();
        });}`
    

    您可以简单地使用 jquery 方法在标题中附加或删除脚本标记。

    要添加 .js 文件,请在 ngOnInit() 下调用以下行:

    $('head').append('<script async src="assets/js/search.js"></script>');
    

    删除 .js 文件:

    document.querySelector('script[src="assets/js/search.js"]').remove();
    

    【讨论】:

      【解决方案4】:

      来自@sivakumar 的回答,我得到了一些使用 .js 文件的提示

      home.html 代码:

      <ion-header>
        <ion-toolbar>
          <ion-title>
            Music Bubble
          </ion-title>
        </ion-toolbar>
      </ion-header>
      
      <ion-content>
        <div id="bubbles">
        </div>
      </ion-content>
      

      home.ts 代码:

      import { Component, OnInit } from '@angular/core';
      declare var bubble: any;
      
      @Component({
        selector: 'app-home',
        templateUrl: 'home.page.html',
        styleUrls: ['home.page.scss'],
      })
      export class HomePage implements OnInit {
      
        constructor() {
        }
      
        ngOnInit(): void {
          bubble();
        }
      }
      

      bubblefile.js 代码:

      var bubble = (function(){
          var nodes = new vis.DataSet([
              {label: "Pop"},
              {label: "Alternative"},
              {label: "Rock"},
              {label: "Jazz"},
              {label: "Hits"},
              {label: "Dance"},
              {label: "Metal"},
              {label: "Experimental"},
              {label: "Rap"},
              {label: "Electronic"},
          ]);
          var edges = new vis.DataSet();
      
          var container = document.getElementById('bubbles');
          var data = {
              nodes: nodes,
              edges: edges
          };
      
          var options = {
              nodes: {borderWidth:0,shape:"circle",color:{background:'#F92C55', highlight:{background:'#F92C55', border: '#F92C55'}},font:{color:'#fff'}},
              physics: {
                  stabilization: false,
                  minVelocity:  0.01,
                  solver: "repulsion",
                  repulsion: {
                      nodeDistance: 40
                  }
              }
          };
          var network = new vis.Network(container, data, options);
      
      
          // Events
          network.on("click", function(e) {
              if (e.nodes.length) {
                  var node = nodes.get(e.nodes[0]);
                  // Do something
                  nodes.update(node);
              }
          });
      })
      

      【讨论】:

      • 像气泡一样更改文件名
      猜你喜欢
      • 2020-04-04
      • 2019-02-11
      • 1970-01-01
      • 2019-11-12
      • 2016-10-01
      • 1970-01-01
      • 2013-03-29
      • 2020-05-06
      • 2020-06-11
      相关资源
      最近更新 更多