我在添加JQuery时创建了Code typescript,并使用requirejs和amd模块进行编译,但是当我在浏览器中启动时出现此错误:
未捕获的TypeError:app.Med.aff不是函数
代码app.ts:
import * as $ from "jquery"
export class Med{
public aff(){
$('#med').click(function(eventObject){
$('#test').show();
})
}
}
code config.ts:
require(
['app'],
function(app: any){
app.Med.aff()
},
function (err: any) {
console.error('ERROR: ', err.requireType);
console.error('MODULES: ', err.requireModules);
}
);
代码app.js:
define(["require", "exports", "jquery"], function (require, exports, $) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Med = (function () {
function Med() {
}
Med.prototype.aff = function () {
$('#med').click(function (eventObject) {
$('#test').show();
});
};
return Med;
}());
exports.Med = Med;
});
code config.js:
"use strict";
require(['app'], function (app) {
app.Med.aff();
}, function (err) {
console.error('ERROR: ', err.requireType);
console.error('MODULES: ', err.requireModules);
});
和Code tsconfig.json:
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "amd", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"noImplicitAny": true,
"strictNullChecks": true,
"outDir": "./build",
"lib": ["es2016","dom"], /* Redirect output structure to the directory. */
"strict": true /* Enable all strict type-checking options. */
},
"exclude": [
"./build",
"./node_modules"
]
}
答案 0 :(得分:0)
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'
@Injectable()
export class YourService {
constructor(private http: Http) { }
getAdvantageData(){
let apiUrl = './assets/data/api/advantage.json';
return this.http.get(apiUrl)
.map( (response: Response) => {
const data = response.json();
return data;
});
}
}
被定义为原型方法。
您需要创建aff
的实例并将其命名为Med
或将new Med().aff()
声明为静态
aff
答案 1 :(得分:0)
@Yury
此代码工作:
代码app.ts:
import * as $ from "jquery"
export class Med{
public aff(){
$('#med').click(function(eventObject){
$('#test').toggle('slow');
})
}
}
let f = new Med()
f.aff();
code config.ts:
require(
['app'],
function(app: any){
},
function (err: any) {
console.error('ERROR: ', err.requireType);
console.error('MODULES: ', err.requireModules);
}
);
谢谢你我的朋友