我想在我的Angular项目中使用Chart.js.在以前的Angular2版本中,我使用'chart.loader.ts'做得很好,它有:
export const { Chart } = require('chart.js');
Run Code Online (Sandbox Code Playgroud)
然后在组件代码中我就是
import { Chart } from './chart.loader';
Run Code Online (Sandbox Code Playgroud)
但升级到cli 1.0.0和Angular 4之后,我收到错误:"找不到名称'require'".
要重现错误:
ng new newapp
cd newapp
npm install chart.js --save
echo "export const { Chart } = require('chart.js');" >> src/app/chart.loader.ts
ng serve
Run Code Online (Sandbox Code Playgroud)
在我的'tsconfig.json'中,我有
"typeRoots": [
"node_modules/@types"
],
Run Code Online (Sandbox Code Playgroud)
在'node_modules/@types/node/index.d.ts'中有:
declare var require: NodeRequire;
Run Code Online (Sandbox Code Playgroud)
所以我很困惑.
顺便说一句,我经常遇到警告:
[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)
Run Code Online (Sandbox Code Playgroud)
虽然我在'.angular-cli.json'中设置了"前缀":"".可能是因为从'angular-cli.json'改为'.angular-cli.json'的原因?
我看过有关在TypeScript代码文件中将"use strict"行放在何处的帖子.我的问题是,为什么要这样呢?
由于TypeScript已经是强类型语言,"use strict"添加了什么?
我正在使用Angular 5,我使用angular-cli创建了一个服务
我想要做的是创建一个服务,读取Angular 5的本地json文件.
这就是我的......我有点卡住......
import { Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
@Injectable()
export class AppSettingsService {
constructor(private http: HttpClientModule) {
var obj;
this.getJSON().subscribe(data => obj=data, error => console.log(error));
}
public getJSON(): Observable<any> {
return this.http.get("./assets/mydata.json")
.map((res:any) => res.json())
.catch((error:any) => console.log(error));
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能完成这个?
我正在使用Angular Maps构建地图应用程序,并希望将JSON文件导入为定义位置的标记列表.我希望在app.component.ts中使用这个JSON文件作为marker []数组.而不是在TypeScript文件中定义硬编码的标记数组.
导入此JSON文件以在我的项目中使用的最佳过程是什么?任何方向都非常感谢!
在TypeScript中,有什么区别
import http = require('http');
Run Code Online (Sandbox Code Playgroud)
和
var http = require('http');
Run Code Online (Sandbox Code Playgroud)
我看到在代码源中使用这两种方式,它们是否可以互换?
我想在我的Angular 2项目中使用libquassel(https://github.com/magne4000/node-libquassel).该库是浏览器化的,所以理论上它应该可以工作,但我不确定如何在我的项目中导入它.
我试着加入我的 typings.d.ts
declare module 'libquassel';
Run Code Online (Sandbox Code Playgroud)
然后导入库
import * as Quassel from 'libquassel';
Run Code Online (Sandbox Code Playgroud)
但我明白了
EXCEPTION: net.Socket is not a function
Run Code Online (Sandbox Code Playgroud)
当我尝试运行我的代码时,我认为这是另一个浏览器嵌入在client/libquassel.js文件中的库.
我该如何使用这个库?
编辑:我会在这里回答所有问题:
ng new proj1再npm install libquassel --save.index.html没有任何其他ng new没有放在那里的东西.import * as Quassel from 'libquassel'和var Quassel = require('quassel')(和那些的排列)导入库,没有任何运气(错误从变化unknown function 'require'到can't find module lib|quassel).重新制作我的项目的步骤:
ng new test
cd test
npm install libquassel --save
ng g s …Run Code Online (Sandbox Code Playgroud)我发现不是很久以前,每当我尝试启动我的应用程序,我收到很多Cannot find name 'require'与Cannot find type definition file for 'node'和Cannot find type definition for 'react-router'
我已经尝试过的一些解决方案是:
1)types: ["node"]在我的 tsconfig.json 中添加一个,如下所示。
2)添加@types/node我的package.json,你们都可以在这里看到。
3)删除node_modules和reing,yarn无济于事。我也删除了 yarn.lock 并且问题仍然存在。
4) 添加一个typeRoots: ["node_modules/@types/node"],这似乎没什么用。
// Package.json
"devDependencies": {
"@types/lodash": "^4.14.110",
"@types/node": "^10.12.18",
"@types/react": "16.3.12",
"@types/react-dom": "15.5.0",
"@types/react-router": "4.0.11",
"babel-core": "6.26.3",
"babel-loader": "7.1.5",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"css-loader": "1.0.0",
"extract-text-webpack-plugin": "2.0.0-beta.4",
"html-webpack-plugin": "2.24.1",
"react": "15.5.4",
"react-dom": "15.5.4",
"react-router-dom": "4.1.1",
"rimraf": "2.6.2",
"style-loader": "0.22.1",
"ts-loader": "1.2.1", …Run Code Online (Sandbox Code Playgroud) 我有一堆简单的.ts文件。不是项目(即独立的 .ts 脚本)。他们使用一些 Node.js 功能。TypeScript 和节点类型定义通过安装
npm install -g typescript
npm install -g @types/node
Run Code Online (Sandbox Code Playgroud)
问题:在 Windows 上我可以tsc foo.ts毫无问题地运行。它可以很好地将 .ts 转换为 .js。但在 Ubuntu 16.04 上,它给了我error TS2304: Cannot find name 'require',error TS2304: Cannot find name 'process'等等。即使添加/// <reference types="node" />到顶部foo.ts或添加--types node开关到 tsc 我也得到error TS2688: Cannot find type definition file for 'node'。
看起来全局安装某些类型在 Windows 中运行良好,但在 Ubuntu 上则不然,所以我假设这不是设计使然。那么有没有办法在全球范围内安装它们呢?或者更准确地说:引用 Ubuntu 上全局安装的类型定义?
我正在使用jsonwebtoken解码令牌,我正在尝试获取到期日期。Typescript 抛出有关该exp属性的错误,我不太确定如何解决它们:
import jwt from 'jsonwebtoken'
const tokenBase64 = 'ey...' /* some valid token */
const token = jwt.decode(tokenBase64)
const tokenExpirationDate = token.exp
// ^^^
// Property 'exp' does not exist on type 'string | object'. Property 'exp' does not exist on type 'string'.
Run Code Online (Sandbox Code Playgroud)
我已经安装@types/jsonwebtoken,并寻找要转换的令牌类型token,但没有找到。建议?
使用
@types/jsonwebtoken@7.2.3jsonwebtoken@8.1.0.tsconfig:
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"jsx": "Preserve",
"moduleResolution": "Node",
"module": "ESNext",
"sourceMap": true,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"target": "ESNext"
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为men.json的文件.
我想做相同的var men = require('./men.json');.
无论我尝试过什么,它都会寻找./men.json.js文件.
我读到我不能使用,import因为它不是ts文件.
什么是等效的要求线?
typescript ×8
javascript ×6
angular ×4
json ×3
node.js ×2
angular-cli ×1
browserify ×1
google-maps ×1
jwt ×1
maps ×1
reactjs ×1
tsconfig ×1
ubuntu ×1