我正在使用browserify和Knockout,并且正在尝试为Knockout组件模板加载html文件。我已经用browserify成功加载了javascript文件,但是我不清楚如何获取它来加载html文件。
我正在尝试做这样的事情:
(function() {
var ko = require('./lib/knockout/dist/knockout.js');
var authenticationViewModel = require('./viewmodels/authentication.js');
var authenticationView = require('./views/authentication.html');
ko.components.register('authentication', {
template: authenticationView,
viewModel: authenticationViewModel
});
})();
但是模板显然没有加载。有人可以向我解释如何实现吗?
这是我在gulpfile.js中拥有的功能,可让browserify与.js文件一起使用:
gulp.task('browserify', function () {
var browserified = transform(function(filename) {
var b = browserify(filename);
return b.bundle();
});
return gulp.src([paths.appJs])
.pipe(browserified)
.pipe(gulp.dest(paths.public));
});
我正在研究html-browserify插件:
https://www.npmjs.com/package/html-browserify
他们使用的示例如下所示:
gulp.task('js', function() {
gulp.src('js/app.js')
.pipe(browserify({
insertGlobals: true,
transform: html
}))
.pipe(concat('app.js'))
.pipe(gulp.dest('./public/js'));
});
但是我不清楚如何将该示例与我当前正在使用的代码协调一致。
最佳答案
brfs应该是一种选择。
在我自己的一个应用程序中,我使用gulp读取一些模板文件,将它们组合到一个对象中,然后编写一个在(浏览器化)应用程序中加载的JSON文件。