[英]Using Javascript and Jquery in a angular 6 cli project
I am using angular 6 cli and i want to use jquery and javascript. 我正在使用angular 6 cli,我想使用jquery和javascript。 i imported jquery and bootstrap but still get an error when trying to use jquery.
我导入了jquery和bootstrap,但是在尝试使用jquery时仍然出现错误。 I want to use the code below or convert it to ts.
我想使用下面的代码或将其转换为ts。 I looked online and followed a couple of suggestions but they didnt work.
我在网上查看并遵循了一些建议,但是它们没有起作用。
when I add the code below to my angular component.ts file it flags error. 当我将以下代码添加到我的angular component.ts文件中时,它会标记错误。
$('.editValues').click(function () {
$(this).parents('tr').find('td.editableColumns').each(function() {
var html = $(this).html();
var input = $('<input class="editableColumnsStyle" type="text" />');
input.val(html);
$(this).html(input);
});
});
the following errors are shown 显示以下错误
ERROR in src/app/_services/user.service.ts(56,5): error TS1003: Identifier expected.
src/app/_services/user.service.ts(56,19): error TS1144: '{' or ';' expected.
src/app/_services/user.service.ts(56,26): error TS1138: Parameter declaration expected.
src/app/_services/user.service.ts(63,4): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
i get these errors when i add that code . 添加该代码时出现这些错误。 ERROR in
发生错误
src/app/_services/user.service.ts(56,5): error TS1003: Identifier expected.
src/app/_services/user.service.ts(56,13): error TS1144: '{' or ';' expected.
src/app/_services/user.service.ts(56,20): error TS1138: Parameter declaration expected.
src/app/_services/user.service.ts(56,31): error TS1005: ';' expected.
src/app/_services/user.service.ts(57,1): error TS1128: Declaration or statement expected.
Your jQuery code is in a class body, where the compiler is expecting a constructor, accessor, property or a method ( src/app/_services/user.service.ts(63,4): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected ). 您的jQuery代码位于类主体中,在该类主体中,编译器期望构造函数,访问器,属性或方法( src / app / _services / user.service.ts(63,4):错误TS1068:意外令牌。构造函数,方法,访问者或属性应为 )。
You need to put it inside a method. 您需要将其放入方法中。
import { Component, OnInit, OnDestroy, OnViewInit } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-my-component',
templateUrl: './app-my-component.html',
styleUrls: ['./app-my-component.css'],
})
export class MyComponent implements OnInit, OnDestroy, OnViewInit {
constructor(private store: Store<any>) {}
ngOnInit() {
// subscribe to store
}
ngOnDestroy() {
// unsubscribe from store
}
ngOnViewInit() {
// Put your jQuery code here
}
}
Note that using jQuery with modern Angular is not really recommended and against best practises. 请注意,实际上不建议将jQuery与现代Angular一起使用,这是最佳做法。 Read more: Best Practice of Angular 2
阅读更多: Angular 2的最佳实践
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.