我使用Angular 6
和scraperjs。
在我的组件文件,使用实例中所给出的代码一样
import { Component, OnInit } from '@angular/core';
import scraperjs from 'scraperjs';
@Component({
selector: 'app-express',
templateUrl: './express.component.html',
styleUrls: ['./express.component.css']
})
export class ExpressComponent implements OnInit {
constructor() { }
ngOnInit() {
this.aliExpress();
}
aliExpress() {
console.log('loaded ali express');
scrapperjs.StaticScraper.create('https://news.ycombinator.com')
.scrape(function ($) {
return $('.title a').map(function () {
return $(this).text();
}).get();
}).then(function (news) {
console.log(news);
});
}
}
但它给错误的
Failed to compile.
./node_modules/cheerio/index.js
Module not found: Error: Can't resolve './package' in '/home/user/code/angular/ali-express/node_modules/cheerio'
跑
npm install
命令来安装依赖的包。我认为,依赖的包安装不正确。
您正在使用require()
加载这不加载在角文件的标准方式的文件。在这里,它会尝试在运行时加载,但无法找到它。
请尝试用进口加载它。如果还是不行,请尝试main.ts
加载它。我不知道,但我装了hammerjs同样的方式。
从您的解决方案文件夹结构中删除nodeModules,然后尝试安装NPM-安装