gpt4 book ai didi

javascript - AngularJs $injector :modulerr after RequireJS r. js 优化

转载 作者:行者123 更新时间:2023-12-03 11:56:46 26 4
gpt4 key购买 nike

正在构建的 AngularJS-RequireJS 应用程序抛出未捕获错误:[$injector:modulerr] 无法实例化模块 myApp,原因是:错误:[$injector:unpr] 未知提供者:r.js 优化后出现错误。以前不存在这个问题。我在这个link中关注了类似的问题并将 ma​​inConfigFile 添加到构建文件中,但问题仍然存在。这是各个文件的代码

build.js

({
mainConfigFile:'js/main.js',
appDir: "./",
baseUrl: "js",
dir: "/home/karthic/optimized-test",
modules: [
{
name: "main"
}
]
})

ma​​in.js

require.config({
baseUrl: './js',
paths: {
angular: 'libs/angular-1.2.9',
angularRoute: 'libs/angular-route-1.2.9'
},
shim: {
'angularRoute': {
deps: ['angular'],
exports: 'angularRoute'
},
'angular': {
exports: 'angular'
}
}

});

require(['angular', 'controller', 'ang_when_routes', 'angularRoute'], function(angular, controller, angWhenRoutes, angularRoute) {
'use strict';

var app = angular.module('myApp', ['ngRoute']);
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});

angWhenRoutes.initRoutes(app);
controller.controllerInit(app);

});

controller.js

define(function() {
function controllerInit(app) {
console.log('inside func cont');
app.controller('mapApp', function($scope, $http, $location) {
console.log('inside mapp,testing r.js optimizer');
});
}
return {
controllerInit: controllerInit
}
});

ang_when_routes.js

define(function() {
function initRoutes(app) {
app.config(
function($routeProvider, $httpProvider) {
$httpProvider.defaults.withCredentials = true;
$routeProvider.
when('/', {
templateUrl: 'index_content.html',
controller: 'mapApp'
})

});
}
return {
initRoutes: initRoutes
}
});

index.html

<!DOCTYPE html>
<html>
<head>
<title>Optimizer error</title>
<script data-main="js/main.js" src="js/libs/require.js"></script>
</head>
<body>
<div ng-view>

</div>
</body>

</html>

index_content.html

<p>Testing the r optimizer</p>

如果可以修复错误,这对我来说真的很有帮助

最佳答案

这是@radimKohler 在第一条评论中建议的答案。为了避免在缩小过程中 AngularJS 出现问题,必须使用 Angular Documentation 中提到的内联数组注释。 。这是修改后的代码

controller.js

define( function () {
function controllerInit(app) {
console.log('inside func cont');
app.controller('mapApp',['$scope','$http','$location', function ($scope, $http, $location) {
console.log('inside mapp,testing r.js optimizer');

}]);

}
return {
controllerInit: controllerInit
}

});

ang_when_routes.js

define(function(){
function initRoutes(app){
app.config(['$routeProvider','$httpProvider',
function($routeProvider,$httpProvider){
$httpProvider.defaults.withCredentials = true;
$routeProvider.
when('/',{
templateUrl:'index_content.html',
controller:'mapApp'
})

}]);

}
return {
initRoutes:initRoutes
}
});

关于javascript - AngularJs $injector :modulerr after RequireJS r. js 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25583686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com