我试图让我的第一个Typescript和DefinitelyTyped节点应用程序启动并运行,并遇到一些错误.
当我尝试转换一个简单的ts节点页面时,我收到错误"TS2304:找不到名称'require'".我已经在SO上阅读了其他几个这样的错误,我认为我没有类似的问题.
我在shell提示符下运行命令:tsc movie.server.model.ts.该文件的内容是:
tsc movie.server.model.ts.
Run Code Online (Sandbox Code Playgroud)
var mongoose = require('mongoose')行引发错误
typings/tsd.d.ts文件的内容是:
'use strict';
/// <reference path="typings/tsd.d.ts" />
/* movie.server.model.ts - definition of movie schema */
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var foo = 'test';
Run Code Online (Sandbox Code Playgroud)
.d.ts文件引用放在相应的文件夹中,并通过命令添加到typings/tsd.d.ts:
/// <reference path="node/node.d.ts" />
/// <reference path="requirejs/require.d.ts" />
Run Code Online (Sandbox Code Playgroud)
生成的.js文件似乎工作正常,所以我可以忽略错误.但我很想知道为什么会出现这种错误以及我做错了什么.
我想在我的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'的原因?
在Angular2中,你可以在那里有一个文件夹/ data /和一个json文件,你可以在localhost:4200/data/something.json访问它.
Angular4中不再可能这样.
任何想法如何让它工作?
我试图加载两种方式的本地JSON文件.
这是我的json文件:
{
"imgsesion": "fa_closesesion.png",
"texthome": "volver a la home",
"logo": "fa_logo.png",
"menu": {
"background": "orange",
"link1": "ESCRITOR",
"link2": "MÚSICO",
"link3": "AYUDA ADMIN",
"submenu": {
"link1": {
"text1": "novelas",
"text2": "obras de teatro"
},
"link2": {
"text1": "compositor",
"text2": "intérprete"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务文件(general.service.ts)
getContentJSON() {
return this.http.get('assets/json/general.json')
.map(response => response.json());
}
Run Code Online (Sandbox Code Playgroud)
这样工作正常,但在Web浏览器控制台中显示下一个错误:
ERROR TypeError: Cannot read property 'menu' of undefined
Run Code Online (Sandbox Code Playgroud)
这是我的服务文件(general.service.ts)
getContentJSON() {
return this.httpClient.get("assets/json/general.json");
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为我找不到general.json文件,它通过错误的控制,它给我一个错误404
这是组件文件(app.component.ts)
export class AppComponent implements OnInit {
contentGeneral: any; …
Run Code Online (Sandbox Code Playgroud) 我想在Angular 7中使用原始加载程序,将文本文件导入TypeScript源代码。我已经找到了大量有关该主题的信息,并花了几个小时试图使其正常工作,但我无法使其正常工作。
我首先创建一个新项目
ng new example --defaults --minimal
Run Code Online (Sandbox Code Playgroud)
我在/src/app/example.txt
其中创建一个文本文件,并显示消息“ Hello World”
然后,我修改app.component.ts
文件以导入此文本文件。
import {Component} from '@angular/core';
import str from 'example.txt';
alert(str);
@Component({
selector: 'app-root',
template: ``,
styles: []
})
export class AppComponent {
}
Run Code Online (Sandbox Code Playgroud)
我Cannot load module "example.txt"
在WebStorm中看到错误,并且在运行时ng build
收到以下错误。
src / app / app.component.ts(2,17)中的错误:错误TS2307:找不到模块“ example.txt”
因此,我用Google搜索了如何解决此问题,并找到了答案。
然后,我添加一个/src/typings.d.ts
包含以下内容的文件,这可以修复WebStorm中的错误。
declare module "*.txt" {
const value: string;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
我ng build
再次运行,错误消息更改为无法找到该模块。
找不到模块:错误:无法在“ C:\ work \ temp \ example …
我试图在typescript类中导入json文件
import * as nationalities from './mock/nationalities.json';
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误
找不到模块'./mock/nationalities.json'.
为了解决这个问题,我添加了这个
declare module '*.json' {
const value: any;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
但它没有解决我的问题,也给了我另一个错误异常
增强中的模块名称无效,无法找到模块'*.json'.
我的打字稿版本
2.9.2
angular ×5
typescript ×3
javascript ×2
json ×2
angular-cli ×1
angular5 ×1
ionic2 ×1
ionic3 ×1
node.js ×1
webpack ×1