在typescript(*.tsx)文件中,我不能用下面的语句导入svg文件:
import logo from './logo.svg';
Transpiler说:[ts]找不到模块'./logo.svg'。 我的svg文件就是<svg>…</svg>。
但在.js文件中,我能够导入它没有任何问题,完全相同的导入语句。我想这与svg文件的类型有关,它必须以某种方式为ts transpiler设置。
你能在ts文件中分享一下如何做到这一点吗?
在typescript(*.tsx)文件中,我不能用下面的语句导入svg文件:
import logo from './logo.svg';
Transpiler说:[ts]找不到模块'./logo.svg'。 我的svg文件就是<svg>…</svg>。
但在.js文件中,我能够导入它没有任何问题,完全相同的导入语句。我想这与svg文件的类型有关,它必须以某种方式为ts transpiler设置。
你能在ts文件中分享一下如何做到这一点吗?
我在我的文件app.spec.ts中有这个导入:
import app from './app';
是什么导致了这个Typescript错误
2:17 error Unable to resolve path to module './app' import/no-unresolved
/应用程序。ts确实存在,但我没有将.ts文件编译成.js文件。一旦我把.ts文件编译成.js文件,错误就消失了。
然而,由于eslint应该与typescript一起工作,它应该用.ts而不是.js来解析模块。
我还在eslint配置文件中添加了typescript信息:
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
我如何配置eslint在这样一种方式,它试图解决模块与.ts而不是.js?
编辑# 1
app.ts的内容:
import bodyParser from 'body-parser';
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { buildSchema } from 'graphql';
const app = express();
const schema = buildSchema(`
type Query {
hello: String
}
`);
const root = { hello: () => 'Hello world!' };
app.use(bodyParser());
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true,
}));
export default app;
在我的代码中,我有一对字符串索引的字典(这里建议)。由于这是一个即兴的类型,我想知道是否有任何建议,我将如何能够循环通过每个键(或值,所有我需要的键无论如何)。感谢任何帮助!
myDictionary: { [index: string]: any; } = {};
我想从一个Angular组件触发HTTP请求,但我不知道如何向它添加URL参数(查询字符串)。
this.http.get(StaticSettings.BASE_URL).subscribe(
(response) => this.onGetForecastResult(response.json()),
(error) => this.onGetForecastError(error.json()),
() => this.onGetForecastComplete()
)
现在是我的静态设置。BASE_URL就像一个没有查询字符串的URL: http://atsomeplace.com/,但我想让它像http://atsomeplace.com/?var1=val1&var2=val2
如何添加var1,和var2到我的HTTP请求对象作为对象?
{
query: {
var1: val1,
var2: val2
}
}
然后只有HTTP模块将其解析为URL查询字符串。
据我所知,当属性是数组时,它的类型可以用两种方式定义。
property_name: type
type可以是哪一种
Array<string>, Array<MyType>, etc. (e.g. let prop1: Array<string>)
and
string[], MyType[], etc. (e.g. let prop1: string[])
这两种情况有什么不同?或者我误解了什么(也许是关于在类型转换中使用的<> ?)
编辑因为这个问题被标记为重复,我知道还有一个关于any[]的问题,但在发布之前我还是看了一下,对我来说,这更多的是关于类型“any”,而不是不同的[]VS <>
我有一个超类,它是许多子类(Customer, Product, ProductCategory…)的父类(Entity)。
我想在Typescript中动态克隆一个包含不同子对象的对象。
例如:拥有不同产品的客户拥有一个ProductCategory
var cust:Customer = new Customer ();
cust.name = "someName";
cust.products.push(new Product(someId1));
cust.products.push(new Product(someId2));
为了克隆对象的整个树,我在实体中创建了一个函数
public clone():any {
var cloneObj = new this.constructor();
for (var attribut in this) {
if(typeof this[attribut] === "object"){
cloneObj[attribut] = this.clone();
} else {
cloneObj[attribut] = this[attribut];
}
}
return cloneObj;
}
当new被转译为javascript时,将引发以下错误:错误TS2351:不能对缺少调用或构造签名的表达式使用'new'。
虽然脚本工作,但我想摆脱转译错误
如何允许在Angular2中访问本地主机之外的对象?我可以在localhost:3030/panel轻松导航,但当我写我的IP如10.123.14.12:3030/panel/时,我无法导航。
您能告诉我怎么修吗?我没有使用npm(节点项目管理-节点安装/节点启动)来安装和运行项目。
如果你需要,我可以提供我的包裹。Json和index.html。
我在Angular中的HTTP有一个问题。
我只是想获得一个JSON列表,并在视图中显示它。
服务类
import {Injectable} from "angular2/core";
import {Hall} from "./hall";
import {Http} from "angular2/http";
@Injectable()
export class HallService {
public http:Http;
public static PATH:string = 'app/backend/'
constructor(http:Http) {
this.http=http;
}
getHalls() {
return this.http.get(HallService.PATH + 'hall.json').map((res:Response) => res.json());
}
}
在HallListComponent中,我从服务中调用getHalls方法:
export class HallListComponent implements OnInit {
public halls:Hall[];
public _selectedId:number;
constructor(private _router:Router,
private _routeParams:RouteParams,
private _service:HallService) {
this._selectedId = +_routeParams.get('id');
}
ngOnInit() {
this._service.getHalls().subscribe((halls:Hall[])=>{
this.halls=halls;
});
}
}
然而,我有一个例外:
TypeError: this.http.get(……)。Map不是[null]中的函数
hall-center.component
import {Component} from "angular2/core";
import {RouterOutlet} from "angular2/router";
import {HallService} from "./hall.service";
import {RouteConfig} from "angular2/router";
import {HallListComponent} from "./hall-list.component";
import {HallDetailComponent} from "./hall-detail.component";
@Component({
template:`
<h2>my app</h2>
<router-outlet></router-outlet>
`,
directives: [RouterOutlet],
providers: [HallService]
})
@RouteConfig([
{path: '/', name: 'HallCenter', component:HallListComponent, useAsDefault:true},
{path: '/hall-list', name: 'HallList', component:HallListComponent}
])
export class HallCenterComponent{}
app.component
import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/hall-center.component";
@Component({
selector: 'my-app',
template: `
<h1>Examenopdracht Factory</h1>
<a [routerLink]="['HallCenter']">Hall overview</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/hall-center/...', name:'HallCenter',component:HallCenterComponent,useAsDefault:true}
])
export class AppComponent { }
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
我写了一些代码:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
我得到一个错误:
JSX元素类型Elem没有任何构造或调用签名
这是什么意思?
我正在Visual Studio代码中的typescript项目中工作,并想隐藏.js。映射(甚至可能是.js)文件,使其不出现在文件资源管理器中。
是否可以在文件资源管理器中只显示.ts文件?