【问题标题】:Failed to use jquery in Angular无法在 Angular 中使用 jquery
【发布时间】:2021-04-19 06:03:39
【问题描述】:

我正在尝试创建一个 +/- 数量框。为此,我在这里使用了 jQuery 代码。当我使用它时,我发现了一个错误。这是我的代码:-

details.component.ts



import { Component, OnInit } from '@angular/core';
declare var $: any;

@Component({
  selector: 'app-details',
  templateUrl: './details.component.html',
  styleUrls: ['./details.component.scss']
})
export class DetailsComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  
    function incrementValue(e) {
      e.preventDefault();
      var fieldName = $(e.target).data('field');
      var parent = $(e.target).closest('div');
      var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
    
      if (!isNaN(currentVal)) {
        parent.find('input[name=' + fieldName + ']').val(currentVal + 1);
      } else {
        parent.find('input[name=' + fieldName + ']').val(0);
      }
    }
    
    function decrementValue(e) {
      e.preventDefault();
      var fieldName = $(e.target).data('field');
      var parent = $(e.target).closest('div');
      var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
    
      if (!isNaN(currentVal) && currentVal > 0) {
        parent.find('input[name=' + fieldName + ']').val(currentVal - 1);
      } else {
        parent.find('input[name=' + fieldName + ']').val(0);
      }
    }
    
    $('.input-group').on('click', '.button-plus', function(e) {
      incrementValue(e);
    });
    
    $('.input-group').on('click', '.button-minus', function(e) {
      decrementValue(e);
    });
    

  }

  

}


details.component.html

<app-home></app-home>
<br/><br/>




<style>
 
input,
textarea {
  border: 1px solid #eeeeee;
  box-sizing: border-box;
  margin: 0;
  outline: none;
  padding: 10px;
}

input[type="button"] {
  -webkit-appearance: button;
  cursor: pointer;
}
</style>







<div class="row container mt-5 mr-5">
<div class="col-3 mr-2">
<h6>CATEGORIES</h6>
<hr>
<small><b>BRACELETS</b></small>
<br/><br/>
<small><b>EARRINGS</b></small>
<br/><br/>
<small><b>NECKLACES</b></small>
<br/><br/>
<small><b>RINGS</b></small>
<br/><br/>


</div>




<div class="col-3">

    
   
</div>


<div class="col-5 offset-1">
   
    
        <p><b>Quantity</b></p>
        <div class="input-group">
            <input type="button" value="-" class="button-minus" data-field="quantity">
            <input type="number" step="1" max="" value="1" name="quantity" class="quantity-field">
            <input type="button" value="+" class="button-plus" data-field="quantity">
          </div>
          
</div>


</div>



 <router-outlet></router-outlet>



angular.json


//other code
           "styles": [
              "node_modules/@fortawesome/fontawesome-free/scss/fontawesome.scss",
              "node_modules/@fortawesome/fontawesome-free/scss/solid.scss",
              "node_modules/@fortawesome/fontawesome-free/scss/regular.scss",
              "node_modules/@fortawesome/fontawesome-free/scss/brands.scss",
              "node_modules/angular-bootstrap-md/assets/scss/bootstrap/bootstrap.scss",
              "node_modules/angular-bootstrap-md/assets/scss/mdb.scss",
              "node_modules/animate.css/animate.css",
              "src/styles.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.cs",
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              "node_modules/chart.js/dist/Chart.js",
              "node_modules/hammerjs/hammer.min.js"
            ]
          }
        },


//other code

tsconfig.json

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}


当我运行这个应用程序时,我发现了这个错误:-

我不明白我如何在 angular10 中使用 jQuery。我将在哪里编写 jQuery 代码?请帮帮我。

【问题讨论】:

  • decrementValue放到你的课外
  • 您显示的错误只是 Typescript 说您没有为参数显式声明 TYPE,因此 ts 将其称为“任何”。它不应该阻止你。让我们回到基础,你为什么认为你需要 Jquery ? 2020 年几乎没有理由使用 JQuery,更不用说在 Angular 项目中了。
  • @Stavm 我需要使用 jquery,因为很多事情。我在脚本中使用了我的 jquery 代码“details.component.html”。但它的代码不起作用。请建议我将在 angular10 中编写 jquery 代码的位置。
  • 实际上,我需要这个 jsfiddle.net/polaszk/1oyfxoor 因为我使用了 jquery。
  • idk 实际问题是什么。首先通过将函数签名从 incrementValue(e) 更改为 incrementValue(e: any) 来解决您在图像中添加的问题。对 ts 抱怨的所有功能做同样的事情。

标签: javascript jquery css angular


【解决方案1】:

当您针对这种特定情况使用 Angular 框架时,我看不出使用 jQuery 的原因。

您应该使用双向绑定和单击事件来触发一切。 Here's a Stackblitz demo.

Ref docs for two-way binding, ref docs for event binding

在我的组件中我有:

export class AppComponent {
  quantity = 0;

  decrementValue() {
    this.quantity > 0 ? this.quantity-- : (this.quantity = 0);
  }

  incrementValue() {
    this.quantity++;
  }
}

在我的 HTML 中(我复制了您的代码并添加了双向绑定和事件处理)

<div class="input-group">
    <input type="button" value="-" class="button-minus" data-field="quantity" (click)="decrementValue()">
    <input type="number" step="1" max="" value="1" name="quantity" class="quantity-field" [(ngModel)]="quantity">
    <input type="button" value="+" class="button-plus" data-field="quantity" (click)="incrementValue()">
</div>

【讨论】:

    【解决方案2】:

    您已在 ngOnInit() {} 中添加了 incrementValue(e) and decrementValue(e) 删除函数,并从 ngOnInit() 中单击 jQuery 并将其放置在 ngOnInit() 之外

    还可以在函数中添加类型,例如incrementValue(e: any)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2015-03-25
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      相关资源
      最近更新 更多