假设我必须开发一个 npm 包my-package,它需要由my-project导入和使用。
在开发my-package 时,我需要从 npm 安装和使用其他包。在my-package开发结束时,我将所有相关文件捆绑在dist目录中。由于我不想将node_modules文件夹中的所有文件作为发布包的一部分,我在.gitignore(或.npmignore)中指定排除node_modules文件夹。所以my-package文件夹的最终结构看起来像
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all the node modules
src
... // all source files
package.json
.gitignore
Run Code Online (Sandbox Code Playgroud)
现在我在 npm 存储库上发布my-package,然后使用命令将其导入my-projectnpm i my-package。
作为这种导入的结果,托管my-project的目录结构类似于
my-project
... // stuff
node_modules
my-package
dist
... // all …Run Code Online (Sandbox Code Playgroud) 我已经下载了模块的类型定义,比方说ModuleA,从@types/module-a.
该模块ADTS文件的样子
declare module "module-a" {
export = moda;
}
declare namespace moda {
interface MODAPromise<T> {
isResolved(): boolean;
....;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的应用程序中,我发现我需要扩展这些类型以及一些额外的规范.
以下建议早些时候收到,我建立我的src目录下的文件模块a.augmented.d.ts这样在这个
declare module "module-a" {
export interface MODAPromise {
myNewProperty: any;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做,TypeScript会发出错误信号"导出分配不能在包含其他导出元素的模块中使用". 排队
export = moda;
Run Code Online (Sandbox Code Playgroud)
的模块ADTS.有没有办法扩展这种类型的声明而无需触及原始的module-adts文件?
我导入了一个库mycoollib,该库驻留在其中node_modules并具有以下文件夹和文件结构
node_modules
- mycoollib
- src
mycoollogic.js
- types
index.d.ts
Run Code Online (Sandbox Code Playgroud)
,其中index.d.ts包含 的类型定义mycoollib,是这样的
/// <reference types="node" />
export type MyCoolType = {
func1: () => void
func2: () => void
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我需要增强MyCoolType类型定义,例如添加第三个函数。所以我像这样在我的文件夹中创建my-cool-type-augmentation.d.ts文件src
declare namespace mycoollib {
export type MyCoolType = {
augmentedFunction: () => void
}
}
Run Code Online (Sandbox Code Playgroud)
但这个解决方案似乎不起作用,因为当我尝试augmentedFunction在 类型的对象上使用时,编译器会发出一个问题信号MyCoolType。
我也尝试过使用模块声明,如下所示
declare module "mycoollib" {
export type MyCoolType = {
augmentedFunction: () => void
} …Run Code Online (Sandbox Code Playgroud) 我有一个 Angular 应用程序,它使用environment.ts文件来设置服务器 url 等内容。
我知道我可以使用中指定的构建配置angular.json在构建时替换文件,例如替换environment.ts为environment.prod.ts.
我想要类似的东西ng serve,例如启动
ng serve --configuration=production
Run Code Online (Sandbox Code Playgroud)
并有environment.prod.ts替换environment.ts.
我这可能吗?是否有更好的方法来使用使用设置的命令行参数进行驱动ng serve?
我正在使用具有子路由的新RC1路由器.这是一个非常简单的路由配置示例.
AppComponent的配置
@Routes([
{path: '/area1/...', component: Area1Component},
{path: '/page2', component: Page2Component},
{path: '/page3', component: Page3Component}
])
Run Code Online (Sandbox Code Playgroud)
Area1Component的配置
@Routes([
{path: '/page11', component: Page11Component},
{path: '/page12', component: Page12Component},
{path: '/page13', component: Page13Component}
])
Run Code Online (Sandbox Code Playgroud)
我可以使用如下命令导航各个页面
this.router.navigate(['page2']) // from AppComponent
Run Code Online (Sandbox Code Playgroud)
要么
this.router.navigate(['../page12'], this.segment) // from pages of Area1 - this.segment is the current segment
Run Code Online (Sandbox Code Playgroud)
我发现,如果我单击浏览器的后退按钮,它会将我带回"一页",但之后它会停留在那里(即我不会导航回导航的整个历史记录).此外,永远不会激活前进按钮.使用旧路由器,我可以来回,没有明显的问题.这适用于Chrome,FF和Safari.
我错过了什么吗?
提前致谢
combineLatest运算符返回一个 Observable,当所有作为参数传入的 observable 完成时,该 ObservablecombineLatest完成。
有没有办法创建一个 Observable,它的行为与返回的 ObservablecombineLatest相同,唯一的区别是当第一个作为参数传递的 Observables完成时它完成?
我有一个简单的案例。AppComponentAngular 应用程序的标准包含ChildComponent在其自己的模块中定义的ChildModule。
的模板ChildComponent很简单
<div class="child" (click)="testClick($event)"></div>
Run Code Online (Sandbox Code Playgroud)
ChildComponent有一个更简单的testClick(event)方法,它只在控制台上记录一条消息。
testClick(event) {
console.log(event);
}
Run Code Online (Sandbox Code Playgroud)
现在我想建立一个AppComponent模拟点击的测试ChildComponent。
这是测试的代码
describe('AppComponent', () => {
let fixture: ComponentFixture<AppComponent>;
let app: AppComponent;
let child: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ ChildModule ],
declarations: [
AppComponent
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
child = fixture.debugElement.query(By.css('.child'));
fixture.detectChanges();
});
it(`should create the child'`, async(() => {
expect(child).toBeTruthy();
}));
it(`clicks on …Run Code Online (Sandbox Code Playgroud) 假设我有一个 Angular 组件,它定义了一个 ObservablemyObs$作为其属性之一。
在一项测试中,给定某些条件,我想测试myObs$不通知。在逻辑上有一些延迟,所以测试必须是异步的。
我正在使用茉莉花。
到目前为止,我已经能够制定这个解决方案:
it('async test', done => {
let dataEmitted;
myObs$
.pipe(
tap(data => dataEmitted = data),
)
.subscribe();
setTimeout(() => {
if (dataEmitted) {
done.fail('should not emit');
} else {
done();
}
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)
但我对此并不满意。我必须依靠setTimeout执行检查和调用done函数。
有关如何正确执行此类测试的任何建议?同步解决方案不起作用,因为逻辑中存在内在的异步性。
我试图在运行时向Angular2路由器添加一些配置.
我正在做的很简单.在AppModule中,我按照指南加载路由器的初始配置
的AppModule
@NgModule({
declarations: [
...
],
imports: [
BrowserModule,
routing,
...
],
providers: [appRoutingProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
appRoutes - app.routes.ts
const appRoutes: Routes = [
{ path: '', component: PocWelcomeComponent },
{ path: '**', component: PageNotFoundComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)
然后在AppComponent onInit()方法中添加一些这样的路由
AppComponent
export class AppComponent implements OnInit {
constructor(
private router: Router
) {}
ngOnInit() {
let routes = this.router.config;
routes.push({ …Run Code Online (Sandbox Code Playgroud) 我正在使用无服务器框架开发一些 Lambda 函数。无服务框架已在全球安装。
我正在使用 Typescript 和 serverless-webpack。
我也在使用 serverless-offline 在本地进行测试。
一切工作正常,除非我尝试从 VSCode 中进行调试。问题是,一旦我从 VSCode 的调试工具启动无服务器离线,所有断点都会变灰。
这是我的配置文件
包.json
{
"name": "backend-serverless",
"version": "1.0.0",
"description": "serverless backend",
"main": "handler.js",
"scripts": {
"test": "mocha -r ts-node/register transform/src/**/*.spec.ts src/**/**/*.spec.ts",
"tsc": "tsc",
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "0.0.34",
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.0",
"chai": "^4.1.2",
"mocha": "^5.0.5",
"serverless-offline": "^3.18.0",
"serverless-webpack": "^5.1.1",
"ts-loader": "^4.1.0",
"ts-node": "^5.0.1",
"typescript": "^2.7.2",
"webpack": "^4.3.0"
}
}
Run Code Online (Sandbox Code Playgroud)
webpack.config.ts
const path = require('path');
const slsw = …Run Code Online (Sandbox Code Playgroud) typescript webpack visual-studio-code serverless-framework serverless-framework-offline
angular ×5
rxjs ×3
typescript ×3
angular-test ×2
jasmine ×2
angular-cli ×1
npm ×1
npm-install ×1
npmignore ×1
serverless-framework-offline ×1
webpack ×1