在我的一个项目中,我将逐步交换js文件。我的一个文件 (hta.js) 包含以下内容:
export function likeUnlikePost(element, $tag, $companyId) {
var elementId = element.target.getAttribute('data-id');
var action = $('#heart-button-' + elementId).attr('action');
if (action == null) {
action = $('#love-heart-button-' + elementId).attr('action');
}
$.post('/posts/ajax/edit/like/' + elementId, {
action: action,
tag: $tag,
companyId: $companyId,
})
.done(function (data) {
var response = data[elementId];
if (response == 'unliked') {
$('#heart-button-span-' + elementId)
.removeClass('htaRed').addClass('htaGrey');
$('#r-heart-button-span-' + elementId)
.removeClass('htaRed').addClass('htaGrey');
$('#r-heart-button-' + elementId).attr('action', 'like');
$('#heart-button-' + elementId).attr('action', 'like');
} else if (response == 'liked') {
$('#heart-button-span-' + elementId)
.removeClass('htaGrey').addClass('htaRed');
$('#r-heart-button-span-' + elementId)
.removeClass('htaGrey').addClass('htaRed');
$('#r-heart-button-' + elementId).attr('action', 'unlike');
$('#heart-button-' + elementId).attr('action', 'unlike');
}
$('#heart-likes-' + elementId)
.html(data['count']);
$('#r-heart-likes-' + elementId)
.html(data['count']);
$('#div-loved-posts')
.html(data['lovedPostsHtml'])
.foundation();
})
.fail(function (data) {
});
}
我想在我的项目的两侧访问此功能。我在 webpack.config.js 中试过这个:
var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('web/assets/')
.setPublicPath('/assets')
.setManifestKeyPrefix('../assets')
.addEntry('hta', './app/Resources/js/hta.js')
.addEntry('app', './app/Resources/js/app.js')
.autoProvideVariables({
'hta': 'hta',
'global.hta':'hta',
})
.enableSassLoader()
.enableSourceMaps(!Encore.isProduction())
.cleanupOutputBeforeBuild()
.configureFilenames({
js: 'js/[name].js',
css: 'css/[name].css',
images: 'images/[name].[ext]',
// images: 'images/[path]/[name].[ext]',
fonts: 'fonts/[name].[ext]'
})
;
module.exports = Encore.getWebpackConfig();
但是,当我尝试通过
document.hta.likeUnlikePost($element, '');
我只是收到一个错误:
TypeError: document.hta is undefined
我以为我在这个例子中尝试过:在此处输入链接描述
但我无法让它正常工作。