简单 AngularJS 应用程序:在 Gruntfile.js 中显示错误

Simple AngularJS application: Showing error in Gruntfile.js

$ grunt connect:development:keepalive

ERROR:

Running "connect:development:keepalive" (connect) task

Warning: undefined is not a function Use --force to continue.

Aborted due to warnings.

我目前正在学习 AngularJS Professional AngularJS (wrox) 这本书的基本代码:

app/index.html

<!DOCTYPE HTML>
<html ng-app="Workflow">
    <head>
        <title>Chapter Two</title>
        <link rel="stylesheet" href="main.css" />
    </head>
    <body ng-controller="ToolsCtrl">
        <h1>Workflow tools from this chapter:</h1>
        <ul>
            <li ng-repeat="tool in tools">{{tool}}</li>
        </ul>

        <script src="bower_components/angular/angular.js"></script>
        <script src="app.js"></script>
    </body>
</html>

app/main.less

html,
body {
  h1 {
    color: steelblue;
  }

app/app.js

'use strict';

angular.module('Workflow', [])
.controller('ToolsCtrl', function($scope) {
        $scope.tools = [
            'Bower',
            'Grunt',
            'Yeoman'
        ];
    });

Gruntfile.js

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        connect: {
            options: {
                port: 9000,
                open: true,
                livereload: 35729,
                hostname: 'localhost'
            },

            development: {
                options: {
                    middleware: function(connect) {
                        return [
                            connect.static('app')
                        ];
                    }
                }
            }
        },

        less: {
            development: {
                files: {
                    'app/main.css': 'app/main.less'
                }
            }
        }
    });

    require('load-grunt-tasks')(grunt);

    grunt.registerTask('default', []);
};

.bowerrc

{
  "directory": "app/bower_components"
}

这些是本书为这个应用程序提供的所有代码。另外,正如书中所问,我 运行 从终端在根文件夹上执行这些命令:

sudo npm install -g bower

sudo npm install -g grunt-cli

sudo npm install --save-dev grunt

sudo npm install --save-dev load-grunt-tasks

sudo npm install --save-dev grunt-contrib-connect

sudo npm install --save-dev grunt-contrib-jshint

sudo npm install --save-dev grunt-contrib-less

sudo npm install --save-dev grunt-contrib-watch

sudo npm install -g less

lessc app/main.less > app/main.css

grunt less

我必须使用 sudo 因为,否则会抛出错误:

Please try running this command again as root/Administrator.

根据这本书,如果我从我的终端 运行 grunt connect:development:keepalive,那么我的浏览器应该启动应用程序 运行ning。但是给我上面提到的错误。

请帮助我解决这个问题,因为我不知道我做错了什么。这就是我包含所有代码的原因。我对这些工具和技术很陌生,我什至不确定我是否应该按照这本书来学习AngularJS。

谢谢。

EDIT:

由于我使用的是 WebStorm IDE,它在 connect.static('app') 行的 Gruntfile.js 中向我显示了一个问题。当我将鼠标悬停在它上面时它显示 Unresolved function or method static()

问题出在connect.static('app')

行的Gruntfile.js是正确的

根据本书网站上的 forum "static" 不再是连接模块的一部分,您将需要 运行 npm install serve-static。之后在 Gruntfile.js 中的 grunt.intConfig 行上方声明一个变量,就像这样...

var serveStatic = require('serve-static');

然后将您的中间件函数更改为如下所示

middleware: function(connect) {
            return [
              serveStatic('app')

希望这对您有所帮助。帮我解决了