簡體   English   中英

如何在我的angular 4.x項目中將js庫用作模塊?

[英]How can I use a js library as a module in my angular 4.x projects?

例如,這是我的test.js文件:

function test() { console.log('Test is working!'); };

這是我旁邊的test.d.ts文件:

declare module 'test' {
export function test(): void;
};

但是,當我嘗試在我的app.component.ts中使用此模塊時,如下所示:

import {Component, OnInit} from '@angular/core';
import * as Test from 'test';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
 title = 'app';
 a = Test;

 ngOnInit() {
  this.a.test();
 }
}

我將看到找不到模塊:錯誤:無法解析“測試”。 順便說一下,這是我的StackBlitz的項目鏈接: 上面項目的鏈接

我會看到錯誤:找不到模塊“測試”。

你必須declare module 'test' {但你是進口Test (請注意大小寫不同test對比Test )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM