简体   繁体   中英

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

For example this is my test.js file:

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

And this is my test.d.ts file right beside it:

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

But when I trying to use this module in my app.component.ts like this:

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();
 }
}

I'll see the Module not found: Error: Can't resolve 'test'. By the way this is my StackBlitz's project link: Link of above project

I'll see the error: Cannot find module 'Test'.

You have declare module 'test' { but you are import Test (notice the case difference test vs. Test ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM