我在使用npm模块chokidar时遇到了一点问题。问题是watcher.getWatched()的值返回null。我是否遗漏了与ES6和此运算符相关的内容?
我有一个条目file.js
let ChokidarWatcher = require('./services/chokidarWatcher');
let FileSystemObject = require('./file-system-object');
module.exports = function (server) {
let studentPath = 'C:/Student2';
let fswWatcher = new ChokidarWatcher(studentPath);
console.log(fswWatcher.GetWatched());
};我有一个ChokidarWatcher类,如下所示
var chokidar = require('chokidar');
var FileSystemObject = require('../file-system-object');
class ChokidarWatcher {
constructor(studentPath) {
this.StudentPath = studentPath;
this.Watcher = chokidar.watch(this.StudentPath, {
// atomic: true,
alwaysStat: true,
usePolling: true,
ignoreInitial: true,
ignored: function (path, stat) {
if (stat) {
var isDir = stat.isDirectory();
var fso = new FileSystemObject(path, stat);
if (isDir) {
return (fso.name === 'node_modules' && studentPath === fso.dir) ||
fso.name === 'bower_components' || /[\/\\]\./.test(path)
} else {
return fso.name === '.DS_Store'
}
}
return false
}
});
}
GetMyWatcher() {
return this.Watcher;
};
GetWatched() {
var items = [];
var watched = this.Watcher.getWatched();
for (var dirpath in watched) {
items.push(new FileSystemObject(dirpath, true))
for (var i = 0; i < watched[dirpath].length; i++) {
var name = watched[dirpath][i];
var path = p.join(dirpath, name);
if (!watched[path]) {
// add file
items.push(new FileSystemObject(path, false))
}
}
}
return items
}
}
module.exports = ChokidarWatcher;发布于 2016-12-21 17:53:45
这个链接很有帮助,我必须等待ready事件
https://stackoverflow.com/questions/41243966
复制相似问题