这里发生了奇怪的事情,使用TypeScript 2.7.2,在带有@ types/express的VSCode版本1.21和随后的代码中,在某些情况下,VSCode会抛出错误,指出"无法调用或构造名称空间样式的导入,并将导致在运行时失败." 但是在具有类似设置和类似tsconfig.json文件的其他机器上,代码才有效..这里发生了什么:
import { Bank } from './Bank';
import * as Express from 'express'; <== errors here..
let app: Express.Express;
this.app = Express(); <== and here
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
TIA,
约翰.
我正在使用jvandemo/generator-angular2-library作为启动器构建一个Angular(2+)组件库,它使用Rollup作为模块构建器.我在库中创建的组件使用MomentJS.
包含MomentJS后,我遇到了各种构建问题.
我首先用于import moment from 'moment';将时刻导入组件,但是在构建时会产生以下错误;
[17:26:28] Starting 'ngc'...
Error at /Users/chris/angular-library/.tmp/components/my-library/my-component.component.ts:6:8: Module '"/Users/chris/my-library/node_modules/moment/moment"' has no default export.
Run Code Online (Sandbox Code Playgroud)
我发现这个问题据说可以使用import * as moment from 'moment';我得到的;
'moment' is imported by build/components/my-component.component.js, but could not be resolved – treating it as an external dependency
events.js:182
throw er; // Unhandled 'error' event
^
Error: Cannot call a namespace ('moment')
at error (/Users/chris/angular-library/node_modules/rollup/dist/rollup.js:185:14)
Run Code Online (Sandbox Code Playgroud)
据我所知,这是唯一的两个选项,我无法工作,我错过了什么?
编辑
我已将此问题添加到库的Github存储库中,其中包含简化复制步骤
我moment.js用来更改应用程序的本地日期格式,但出现以下错误:
导入库时,“ moment”没有导出的成员“ default”。
下面是我的代码:
import {Inject, Injectable, Optional} from '@angular/core';
import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material';
import * as _moment from 'moment';
import {default as _rollupMoment, Moment} from 'moment';
const moment = _rollupMoment || _moment;
Run Code Online (Sandbox Code Playgroud) 我正在努力在Angular 2 Typescript应用程序中使用moment.js库.即使在阅读了这个问题的答案之后,我也无法让它发挥作用.
这是我到目前为止所做的:
我配置System.js来检索时刻库:
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
},
moment: {
main: 'moment.js',
type: 'cjs',
defaultExtension: 'js'
}
},
map: {
moment: 'node_modules/moment'
}
});
Run Code Online (Sandbox Code Playgroud)typings install moment-node --ambient --save和安装了打字稿打字typings install moment --ambient --save,所以我可以在打字/主/环境/时刻节点和打字/主/环境/时刻看到正确的打字现在,如果在我的代码中我使用import * as moment from 'moment';typescript编译运行顺利,我可以在Atom编辑器中看到正确的建议(如果我开始moment().我可以看到year(),month()等).但是,如果我在浏览器中运行我的代码,它会给出一个错误,说'时刻不是一个函数'(调试我可以看到那个时刻是一个有很多方法的对象).
如果我import moment from 'moment';在浏览器中编写代码运行正常,但是typescript编译失败,"模块时刻没有默认导出",并且在编写代码时我无法从Atom获得任何建议.
我究竟做错了什么?在Angular 2打字稿应用程序中导入moment.js(和任何非打字稿库)的正确方法是什么?
我在如何使用时刻-js-library-in-angular-2-typescript-app中采用的方法非常接近,但仍然得到error TS2307: Cannot find module 'mqtt'.
npm install --save mqtt
<s>typings install --save mqtt</s
Run Code Online (Sandbox Code Playgroud)
没有找到打字但这确实......
typings install mqtt --save --ambient
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.conf看起来像这样
{
"compilerOptions": {
"noImplicitAny": true,
"module": "commonjs",
"target": "ES5",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"declaration": true
},
"files": [
"ng2-mqtt.ts"
],
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
而ng2-mqtt.ts只是有这个...
export * from './src/mqtt.service'
Run Code Online (Sandbox Code Playgroud)
并./src/mqtt.service.ts有..
import {Injectable} from 'angular2/core';
import * as mqtt from 'mqtt';
@Injectable()
export class MqttService {
constructor() {
//mqtt.connect('ws://10.0.1.100:3333')
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试将moment.js导入到我的angular2应用程序中.首先,我已经按照本指南,我找到了这个答案,但即使我试图按照建议的解决方案,我仍然无法导入moment.js.包存在,实际上我的IDE(visual studio)能够毫无问题地找到moment.d.ts文件,但是当我用npm start运行我的应用程序时,我在控制台中得到了这两个错误:
"NetworkError:404 Not Found - http:// localhost:3000/node_modules/moment / " / node_m...moment/错误:patchProperty/desc.set/wrapFn @ http:// localhost:3000/node_modules/zone.js /dist/zone.js:769:27 Zonehttp:// localhost:3000/node_modules/zone.js/dist/zone.js:356:24 Zonehttp:// localhost:3000/node_modules/zone.js/dist/zone .js:256:29 ZoneTask/this.invoke @ http:// localhost:3000/node_modules/zone.js/dist/zone.js:423:29
加载http:// localhost:3000/node_modules/moment时出错 "时刻"来自'我的档案'
我试图以这种方式导入时刻
import * as moment from 'moment'
Run Code Online (Sandbox Code Playgroud)
并以这种方式
import * as moment from 'moment/moment.d'
Run Code Online (Sandbox Code Playgroud)
但没什么,我仍然得到错误.我的SystemJs'map属性就是这个
var map = {
'app': 'app', // 'dist',
'@angular': 'node_modules/@angular',
'services': 'app/service',
'moment': 'node_modules/moment',//moment.js
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'rxjs': 'node_modules/rxjs'
};
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用打字安装,我会收到此错误:
"moment"的类型已存在于"node_modules/moment/moment.d.ts"中.您应该让TypeScript解析打包的打字并卸载Typings安装的副本
所以我已经卸载它,并重新键入命令,我得到这个错误
打字ERR!message无法在注册表中找到"moment"("npm").
如何摆脱这种困境?
我在这里遵循日期过滤的文档:https :
//www.ag-grid.com/javascript-grid-filter-date/#gsc.tab=0
但是,当我尝试过滤日期时,它不起作用. 过滤后的结果与之前相同,而其他列如文本和数字则起作用。核心问题应该是在Date对象里面gridOptions。我将 Angular 日期管道的日期显示格式化为与 ag-grid 的过滤格式相同的格式,但实际的日期数据是这样的:“2017-12-19T08:29:19Z”。不知道会不会影响过滤方式。
版本:Angular 4+、ag-grid 15.0.0、ag-grid-angular 15.0.0
这是我的代码:
import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
import { GridOptions, GridApi } from "ag-grid";
import { ApiService } from '../../services/api.service/api.service';
import * as Models from '../../models/models';
@Component({
selector: 'app-orders',
templateUrl: './orders.component.html',
styleUrls: ['./orders.component.scss']
})
export class OrdersComponent implements OnInit {
gridOptions: GridOptions;
gridApi: GridApi;
gridColumnApi;
rowSelection;
orders: Models.Order[] = [];
constructor(
private apiService: ApiService,
private modalService: NgbModal, …Run Code Online (Sandbox Code Playgroud) 昨天我一直在研究我的角度2应用程序,一切都运行良好.今天我正在尝试npm start我的应用程序,我得到这个编译错误:
typings/browser/ambient/moment/index.d.ts(9,21): error TS2503: Cannot find namespace 'moment'.
Run Code Online (Sandbox Code Playgroud)
虽然我没有moment在我的应用程序中提及任何相关内容!
如果我没有使用任何相关内容,会导致此错误的原因是什么moment?
我试过了:
npm remove moment有任何想法吗?
我按照这个例子如何在Ionic 2 RC5中导入和使用时刻.
但它无法找到该文件.我的improt在src/index.html中:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Ionic App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#4e8ef7">
<!-- cordova.js required for cordova apps -->
<script src="cordova.js"></script>
<!-- un-comment this code to enable service worker
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.log('Error', err));
}
</script>-->
<link href="build/main.css" rel="stylesheet">
</head>
<body>
<!-- …Run Code Online (Sandbox Code Playgroud)