gpt4 book ai didi

angularjs - 简单的 AngularJS 应用程序 : Showing error in Gruntfile. js

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:26 24 4
gpt4 key购买 nike

$ 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.

我目前正在从 Professional AngularJS (wrox) 一书中学习 AngularJS,这里是书中的基本代码:

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

减少咕噜声

我不得不使用 sudo 因为,否则它会抛出错误:

Please try running this command again as root/Administrator.

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

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

谢谢。

EDIT:

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

最佳答案

问题出在 Gruntfile.js 的 connect.static('app') 行是正确的

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

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

然后把你的中间件函数改成这样

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

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

关于angularjs - 简单的 AngularJS 应用程序 : Showing error in Gruntfile. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33354807/

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