【发布时间】: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