我需要加载jquery1.7作为模块,我已经看到了此代码 @Jrburke:

requirejs.config({
  paths: {
    'jquery' : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min'
  }
});

require(['jquery'], function($) {
  //$ points to jQuery
});

这对我来说不是很有用,因为所有人 .js 名称是由服务器端生成的,我从php array中获得了它们。

所以,我写了这篇文章:

require(['http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'],
        function($) {
  //$ points to jQuery
});

$ 在此功能中为无效。

更新:

这是我的php-template,它为此页面渲染了我的JS脚本:

<script src="http://requirejs.org/docs/release/1.0.1/minified/require.js">
</script>

<script> 
    require([
        <?php echo "'". implode("',\n\t'", $this->scripts) . "'\n"; ?>
    ], function($){

        console.warn ($); // null ;(

        // loaded jQuery
        window.$ = $;

        // Load main client script for this page
        boot( '<?php echo $this->eprint($this->content_page); ?>' );

    });
</script>

这是我此页面的PHP阵列(页面 index):

$scripts = array(
    'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js',
    'http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
    '/js/libs/jquery.history.js?v=1321687090',
    '/js/libs/coolclock.js?v=1321629683',
    '/js/libs/excanvas.js?v=1321629683',
    '/js/client.modules.js?v=1321703735',
    '/js/client.all.js?v=1322512192',
    '/js/boot.js?v=1322512037',
    '/js/client.index.js?v=1321689884'
);
有帮助吗?

解决方案

具有表格的PHP数组:

$jquery = array (
   'jQuery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'
);

然后尝试:

requirejs.config({
  paths: <?php echo json_encode($jquery) ?>
});

require(['jquery'], function($) {
  //$ points to jQuery
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top