【问题标题】:Require is not defined in AngularJSAngularJS 中没有定义要求
【发布时间】:2018-08-29 01:44:45
【问题描述】:

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

appGroup.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,我想在其中需要这两个文件,怎么做?

【问题讨论】:

    标签: javascript angularjs require


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢妈妈,我怎样才能通过“注射”来做到这一点?请提出建议。
    • 无论你想使用什么模块,下载 npm 模块或文件或在线链接在 index html 文件的 script 标记中的路径,然后在模块中的 Angular 应用程序中添加依赖项,如例如:app.module('app1',['dependacy1'])
    • 谢谢。这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-12-26
    • 2015-11-03
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多