也许我不理解它,但是如果我有一个服务并且我从中捕获了任何错误,那么为什么会zone.js抛出 404 not found,而我捕获错误并返回 json 对象,这样客户端就会知道该怎么做。如何避免控制台中的错误?我可以吗?
例如:
return this.http.post(this.callUrl, requestBody)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
private handleError (error: Response | any) {
let body = error.json();
return Observable.throw(body);
}
Run Code Online (Sandbox Code Playgroud)
我想替换嵌套对象中的键的空格.我有一个对象如下:
var data =
{ 'General Information':
{ 'Referral No': '123123',
Marketer: '',
Casemanager: 'Alexis Clark',
'CM Username': '',
VOC: '',
'Foreign Voluntary': '',
},
'Account Name': 'CTS Health',
}
Run Code Online (Sandbox Code Playgroud)
我做的是:
for (var k in data) {
if (k.replace(/\s/g, '') !== k) {
data[k.replace(/\s/g, '')] = data[k];
if (data[k] !== null && typeof data[k] === 'object') {
for (var x in data[k]) {
if (x.replace(/\s/g, '') !== x) {
data[k][x.replace(/\s/g, '')] = data[k][x];
delete data[k][x];
}
}
}
delete data[k]; …Run Code Online (Sandbox Code Playgroud) 我似乎无法理解我在客户端应用程序上遇到的错误.我正在订阅graphql订阅,我能够检索更新,但我无法将更改推送到名为"models:ModelClass []"的视频脚本数组,该数组绑定到视图.
有什么我遗失或做错了吗?
models.component.ts
this.apollo.subscribe({
query: gql`
subscription {
newModelCreated{
_id
name
type
train_status
deploy_status
data_path
description
created_at
updated_at
}
}
`
}).subscribe((data) => {
console.log("CREATED: " + JSON.stringify(data.newModelCreated));
console.log(data.newModelCreated);
var temp:ModelClass = data.newModelCreated;
this.models.push(temp);
});
Run Code Online (Sandbox Code Playgroud)
模型class.ts
export interface ModelClass {
_id: string;
name: string;
type: string;
parameters: {
alpha: number;
};
train_status: string;
deploy_status: string;
test_accuracy: string;
created_at: number;
updated_at: number;
}
Run Code Online (Sandbox Code Playgroud) 我希望能够不必在我使用该模块中的值的所有位置前面键入模块名称(甚至是模块的别名)。(我宁愿不必求助于运行 C 预处理器,这样我就可以做 #include 类型的事情。)
(关闭但没有雪茄:Angular 2 import 语句通配符语法)
我想
import * from "./MyModule";
console.log( foobar );
Run Code Online (Sandbox Code Playgroud)
而不是烦人的东西,比如:
console.log( MyModule.foobar )
Run Code Online (Sandbox Code Playgroud)
或者:
import * as M from "./MyModule";
console.log( M.foobar );
Run Code Online (Sandbox Code Playgroud) 我知道JavaScript中的'this'与TypeScript中的含义不同,根据TypeScript中的这篇文章'this'.我在JavaScript中使用以下代码用于在所选节点上创建较粗的笔划,并为所有其他节点提供较小的笔划.
node.on('click', function (d) {
d3.selectAll('circle').attr('stroke-width', 1.5);
d3.select(this).select('circle').attr('stroke-width', 5);
})
Run Code Online (Sandbox Code Playgroud)
在TypeScript我有
this.node.on('click', (d:any) => {
this.node.selectAll('circle').attr('stroke-width', 1.5);
[this is where I need help].select('circle').attr('stroke-width', 5);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的Web应用程序安装打字稿键入,但无法获得命令提示符以识别该typings命令。
typings install 引发以下错误:
'typings' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
当我运行时npm i typings -g,控制台会告诉我模块已安装。但是当我查看全局npm目录时,没有打字文件。
C:\gitdev\MyWebApp\src\main\webapp\myUI>npm i typings -g
C:\home\123456\AppData\Roaming\npm\typings -> C:\home\123456\AppData\Roaming\npm\node_modules\typings\dist\bin.js
C:\home\123456\AppData\Roaming\npm
`-- typings@2.1.1
Run Code Online (Sandbox Code Playgroud)
123456为了安全起见,我的用户名已被替换为。其他所有内容均逐字复制。
我没有管理员权限(我在工作计算机上)
where typings 还会引发错误(信息:找不到给定模式的文件。
npm -g对于grunt-cli效果很好
npm在我的PATH中 C:\Users\123456\AppData\Roaming\npm
我可能会误会,但是在操场上看打字稿时,我注意到它们将类的方法和对象变量包装在一起,感觉每次我调用一个新对象时,它可能会降低性能。
例如Class的Typescript游乐场输出
var FatObject = (function () {
function FatObject(thing) {
this.objectProperty = 'string';
this.anotherProp = thing;
}
FatObject.prototype.someMassivMethod = function () {
//many lines of code
//...
//...
//...
//.......................
};
return FatObject;
}());
var thing = 'example';
var objOne = new FatObject(thing);
var objTwo = new FatObject(thing);
var objThree = new FatObject(thing);
var objFour = new FatObject(thing);
Run Code Online (Sandbox Code Playgroud)
我觉得如果我编写大量方法,那么每次我从类中创建一个新对象时,我也会编译这些大量方法。
创建新对象时,无需在函数名称之外声明方法。
例如老方法:
function ThinObj(thing) {
this.anotherProp = thing;
this.objectProperty = 'string';
}
ThinObj.prototype.someMassivMethod = function() {
//many lines of code …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Typescript/React 中使用一个简单的 JS 库,但无法为其创建定义文件。该库是 google-kgsearch ( https://www.npmjs.com/package/google-kgsearch )。它以 CommonJS 风格导出单个函数。我可以成功导入和调用该函数,但无法弄清楚如何将参数类型引用到结果回调。
这是大部分库代码:
function KGSearch (api_key) {
this.search = (opts, callback) => {
....
request({ url: api_url, json: true }, (err, res, data) => {
if (err) callback(err)
callback(null, data.itemListElement)
})
....
return this
}
}
module.exports = (api_key) => {
if (!api_key || typeof api_key !== 'string') {
throw Error(`[kgsearch] missing 'api_key' {string} argument`)
}
return new KGSearch(api_key)
}
Run Code Online (Sandbox Code Playgroud)
这是我对其进行建模的尝试。大多数接口对服务返回的结果进行建模:
declare module 'google-kgsearch' {
function KGSearch(api: string): KGS.KGS;
export = …Run Code Online (Sandbox Code Playgroud) javascript commonjs typescript typescript-typings typescript2.0
我正在围绕 Express JS 创建一个抽象层,但遇到了问题。考虑以下代码(见底部),当调用 registerRoute 并点击该行时:
expressFnc(route, (req: any, res: any, next: any) => dummy(req, res, next));
Run Code Online (Sandbox Code Playgroud)
我遇到了错误:
无法读取未定义的属性“lazyrouter”
在这种情况下,我认为“this”适用于 ExpressJS (lib/application.js:479:10)。
const express = require("express");
export class ExpressHttpHost implements IHttpHost {
private host: any = express();
private contentResolver?: ContentResolver;
public configure(contentResolver: ContentResolver): void {
this.contentResolver = contentResolver;
}
public registerRoute(
route: string,
verb: HttpVerb,
handler: (request: HttpRequest) => HttpResponse,
): void {
switch (verb) {
case HttpVerb.Get:
this.createDummyRequest(this.host.get, route, handler);
break;
case HttpVerb.Delete:
this.createDummyRequest(this.host.delete, route, handler);
break;
case HttpVerb.Options: …Run Code Online (Sandbox Code Playgroud) 我想从角度 7 更新到角度 8。这就是我所做的。首先我跑了yarn upgrade --latest。我注意到这会给我 typescript 3.6.3,这对于 Angular 来说太新了,所以我将其降级到 3.5.3。现在,当我尝试运行我的应用程序时,我收到很多错误(例如 @ViewChild 缺少参数)
所以,我想,这可以通过以下方式解决ng update -all:
$> ng update --all
Using package manager: 'yarn'
Collecting installed dependencies...
Found 66 dependencies.
Package "@angular/compiler-cli" has an incompatible peer dependency to "typescript" (requires ">=3.4 <3.6", would install "3.6.3").
Incompatible peer dependencies found.
Peer dependency warnings when installing dependencies means that those dependencies might not work correctly together.
You can use the '--force' option to ignore incompatible peer dependencies and …Run Code Online (Sandbox Code Playgroud) javascript ×7
typescript ×5
angular ×2
angular-cli ×1
commonjs ×1
d3.js ×1
es6-modules ×1
express ×1
node.js ×1
npm ×1
npm-install ×1
oop ×1
prototype ×1
rxjs ×1