在 encore(旧版应用程序)之外访问 encore-js-File
Access encore-js-File outside of encore (legacy app)
在我的一个项目中,我将逐步交换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
我想我在这个例子中尝试过:enter link description here
但我无法让它正常工作。
你必须在调用它们之前导入你的函数。
希望对你有所帮助。
2 种可能性:
1 /像你一样使用出口。因此,从 webpack 中删除文件并从“文件路径”调用 import {}。这里 app/Resources/assets/hta.js
2/ 现在使用 webpack 删除导出函数并在 let 中声明你的函数我认为你可以直接调用你的函数
在两种情况下不要省略 args。
再见
在我的一个项目中,我将逐步交换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
我想我在这个例子中尝试过:enter link description here
但我无法让它正常工作。
你必须在调用它们之前导入你的函数。
希望对你有所帮助。
2 种可能性: 1 /像你一样使用出口。因此,从 webpack 中删除文件并从“文件路径”调用 import {}。这里 app/Resources/assets/hta.js 2/ 现在使用 webpack 删除导出函数并在 let 中声明你的函数我认为你可以直接调用你的函数
在两种情况下不要省略 args。
再见