简体   繁体   English

使用Webpack Encore在Angular Datatables中找不到DataTable

[英]DataTables not found in Angular Datatables using Webpack Encore

I've been upgrading a Symfony application from 3.4 to 4.2.2 , everything is fine but I cannot get DataTables to function via yarn install and webpack encore with angular-datatables . 我一直在将Symfony应用程序从3.4升级到4.2.2 ,一切都很好,但我无法通过yarn install和带有angular-datatables的 webpack再现来使DataTables正常运行。

How I've set it up 我是如何设置它的

Yarn install : 纱线安装

yarn add jquery@2.1.4
yarn add angular@1.4.8
yarn add datatables.net@1.10.19
yarn add datatables.net-dt@1.10.11
yarn add angular-datatables@0.6.2

Added those files to my Webpack app.js along with jQuery: 将这些文件与jQuery一起添加到我的Webpack app.js

var $ = require('jquery');
require('datatables.net');
require('datatables.net-dt');
require('angular');
require('angular-datatables');

Included the app.js inside of my Webpack config file: 包含我的Webpack配置文件中的app.js

var Encore = require('@symfony/webpack-encore');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')

    .addEntry('app', './assets/js/app.js')

    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

and included the above in my front-end template via: 并通过以下方式将上述内容包含在我的前端模板中:

{{ encore_entry_script_tags('app') }}

The Error 错误

The above results in the following Javascript error which seems to show the DataTables API is not accessible. 以上结果导致以下Javascript错误,似乎表明DataTables API无法访问。

Uncaught TypeError: Cannot read property 'Api' of undefined
at initAngularDataTables (angular-datatables.js:478)
at Object.invoke (angular.js:4523)


/* @ngInject */
function initAngularDataTables() {
    if ($.fn.DataTable.Api) {
        /**
         * Register an API to destroy a DataTable without detaching the tbody so that we can add new data
         * when rendering with the "Angular way".
         */
        $.fn.DataTable.Api.register('ngDestroy()', function(remove) {
            remove = remove || false;




Possible Solution 可能解决方案

I've tried the following hacky fix but it means I have to have my Angular code in the same app.js file else it doesn't work. 我尝试了以下hacky修复,但这意味着我必须在相同的app.js文件中使用我的Angular代码,否则它不起作用。 It also means some functions still fail... 这也意味着一些功能仍然失败......

global.$ = global.jQuery = require('jquery');

require('jquery-ui');
require('bootstrap');
require('admin-lte');
require('datatables.net-dt');
global.moment = require('moment');

$.fn.dataTable = $.fn.DataTable = global.DataTable = require('datatables.net');

Error : 错误

TypeError: _oTable.ngDestroy is not a function
    at _destroyAndCompile (angular-datatables.js:947)

Your project has datatables.net and datatables.net-dt as dependency, and both of them has their own dependency (jQuery>1.7) . 你的项目有datatables.netdatatables.net-dt作为依赖项,它们都有自己的依赖项(jQuery>1.7) If you don't specify a jQuery version for your project, your dependency tree end up with the latest version of jQuery which is also used by your dependencies. 如果没有为项目指定jQuery版本,则依赖关系树会以最新版本的jQuery结束,jQuery也会被依赖项使用。

├─jquery@3.3.1
├─datatables.net@1.10.19
└─datatables.net-dt@1.10.11

But if you specify an old version of jQuery, your dependency tree end up fetching the latest version of jQuery for your dependencies (yarn does it, npm does not, I don't know why). 但是如果你指定一个旧版本的jQuery,你的依赖树最终会为你的依赖项获取最新版本的jQuery(yarn会这样做,npm不会,我不知道为什么)。

├─jquery@2.1.4
├─datatables.net@1.10.19
│      └─jquery@3.3.1
└─datatables.net-dt@1.10.11
       └─jquery@3.3.1

As a result, you have 2 different jQuery in your porject. 因此,您的项目中有2个不同的jQuery。

const jQuery = require('jquery')
console.log(jQuery.fn.jquery) // logs 2.1.4
const DataTable = require('datatables.net')
console.log(DataTable.$.fn.jquery) // logs 3.3.1

As DataTable binds itself to an encapsulated instance of jQuery, jQuery.fn.dataTable is obviously undefined . 由于DataTable将自身绑定到jQuery的封装实例,因此jQuery.fn.dataTable显然是undefined To fix this issue, just use the latest version of jQuery in your project. 要解决此问题,只需在项目中使用最新版本的jQuery即可。 Or you can recommend yarn to use a fixed version of jQuery for all of it's dependencies adding a resolution directive to your package.json file. 或者你可以推荐使用固定版本的jQuery来为它的所有依赖项添加一个解析指令到你的package.json文件。

"dependencies": {
    "angular": "1.4.8",
    "angular-datatables": "0.6.2",
    "datatables.net": "1.10.19",
    "datatables.net-dt": "^1.10.19",
    "jquery": "2.1.4"
},
"resolutions": {
    "jquery": "2.1.4"
}

Then execute yarn install command, and your unwanted jQuery installations will be taken care of. 然后执行yarn install命令,将处理您不需要的jQuery安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM