gpt4 book ai didi

javascript - AngularJS 中未定义 Require

转载 作者:行者123 更新时间:2023-11-30 11:22:14 24 4
gpt4 key购买 nike

我有两个模块,即“用户”和“组”,并且在这两个模块中都有不同的路由。现在我想在其他一些模块中使用它们,我试图要求这两个模块,但出现错误,因为 required is not defined。我该如何解决?这是我的代码:

应用组.js

    let myNinjaAppforGroup = angular.module('myNinjaAppforGroup',['ngRoute']);
//injected ngRoute module as a dependency in the above statement

myNinjaAppforGroup.config(['$routeProvider',function($routeProvider)
{
//code executes before application runs

$routeProvider
.when('/group/home',{
templateUrl:'views/home.html', //which view to be rendered if user visits this url
})
.when('/group/directory',{
templateUrl:'views/directory.html',
controller:'NinjaController'//it will be the controller for the mentioned route
}).otherwise({
redirectTo:'/group/home'
});
}]);

myNinjaAppforGroup.run(function()
{
//code executes when application runs
});

myNinjaAppforGroup.controller('NinjaController',['$scope','$http',function($scope,$http){
//scope is the glue between controller and view. Its also a dependency
$scope.message="Hey Angular!";//this is accessable in views
$scope.removeNinja = function(ninja)
{
let removedNinja = $scope.ninjas.indexOf(ninja);
$scope.ninjas.splice(removedNinja,1);
}
$scope.addNinja = function()
{
$scope.ninjas.push({
name:$scope.newNinja.name,
rate:parseInt($scope.newNinja.rate),
belt:$scope.newNinja.belt,
available:true
});
$scope.newNinja.name="";
$scope.newNinja.rate="";
$scope.newNinja.belt="";
}

$http.get('model/ninjas.json').then(function(response){
$scope.ninjas=response.data;
//console.log(response); for checking the object received
//whatever data we are getting from the http service is being saved here.
})

}]);

module.exports = myNinjaAppforGroup;

`and appUsers.js`

let myNinjaAppforUsers = angular.module('myNinjaAppforUsers',['ngRoute']);
//injected ngRoute module as a dependency in the above statement

myNinjaAppforUsers.config(['$routeProvider',function($routeProvider)
{
//code executes before application runs

$routeProvider
.when('/user/home',{
templateUrl:'views/home.html', //which view to be rendered if user visits this url
})
.when('/user/directory',{
templateUrl:'views/directory.html',
controller:'NinjaController'//it will be the controller for the mentioned route
}).otherwise({
redirectTo:'/user/home'
});
}]);

myNinjaAppforUsers.run(function()
{
//code executes when application runs
});

myNinjaAppforUsers.controller('NinjaController',['$scope','$http',function($scope,$http){
//scope is the glue between controller and view. Its also a dependency
$scope.message="Hey Angular!";//this is accessable in views
$scope.removeNinja = function(ninja)
{
let removedNinja = $scope.ninjas.indexOf(ninja);
$scope.ninjas.splice(removedNinja,1);
}
$scope.addNinja = function()
{
$scope.ninjas.push({
name:$scope.newNinja.name,
rate:parseInt($scope.newNinja.rate),
belt:$scope.newNinja.belt,
available:true
});
$scope.newNinja.name="";
$scope.newNinja.rate="";
$scope.newNinja.belt="";
}

$http.get('model/ninjas.json').then(function(response){
$scope.ninjas=response.data;
//console.log(response); for checking the object received
//whatever data we are getting from the http service is being saved here.
})

}]);

module.exports = myNinjaAppforUsers;

现在我有另一个文件 app.js,我想在那里 require 这两个文件,怎么办?

最佳答案

Require 在浏览器中不起作用。基本上 require 是一个 node_module,我们可以通过它访问其他模块或文件。因此,如果您在浏览器端使用它,请尝试其他操作,例如 import 或 self.import 或 injecting。

文档:http://requirejs.org/docs/download.html

将此添加到您的项目中:http://requirejs.org/docs/release/2.2.0/minified/require.js

看看这个http://requirejs.org/docs/api.html

关于javascript - AngularJS 中未定义 Require,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49380917/

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