我开始学习node.js. 我想使用websockets创建一个聊天应用程序.在控制台中,它工作正常.但是在浏览器中我得到了
未捕获的ReferenceError:未定义require
我用Google搜索并看到了这个答案.我想尝试浏览,但它不能正常工作.这是我的js文件
client.js:
var WebSocket = require('websocket');
var connector;
var WebSocketClient = WebSocket.client;
var client = new WebSocketClient();
client.on('connectFailed', function (error) {
console.log('Connect Error: ' + error.toString());
setTimeout(self.connect, 2000);
});
client.on('connect', function (connection) {
connector = connection;
console.log('WebSocket Client Connected');
connection.on('error', function (error) {
errorStr = error.toString();
console.log("Connection Error: " + errorStr);
});
connection.on('close', function () {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function (message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + …Run Code Online (Sandbox Code Playgroud) 我在React(coffee jsx)中有以下组件定义:
CompUtils = require './../utils/comp-utils'
...
render: ->
console.log CompUtils # prints {} (empty object)
<div>
Then
{CompUtils.getConstructComponent @props.construct, @props.onUpdate, @props.onRemove}
</div>
Run Code Online (Sandbox Code Playgroud)
但这有效:
render: ->
console.log require('./../utils/comp-utils')
<div>
Then
{require('./../utils/comp-utils').getConstructComponent @props.construct, @props.onUpdate, @props.onRemove}
</div>
Run Code Online (Sandbox Code Playgroud)
我对此绝对感到困惑.请注意,CompUtils已成功用于其他组件.
我正在浏览一些ES6/browserify教程,我看到类似的东西:
import 'jquery';
import domready from 'domready';
Run Code Online (Sandbox Code Playgroud)
进口和进口有什么区别?
如果我只使用导入domready - domready无法正常工作.
我想在我的vueify组件的script标签中使用vue-resource $ http方法,但我总是得到这个错误:
Uncaught TypeError: Cannot read property 'get' of undefined
Run Code Online (Sandbox Code Playgroud)
我的猜测是"this"关键字不起作用(不知道原因)或模块安装不正确(尽管应该检查).我的vue组件看起来像这样:
<template>
<!-- just displaying the data -->
</template>
<script>
module.exports = {
data: function () {
return {
foo: "bar"
}
},
ready: function() {
// the error occurs on the next line
this.$http.get('/api/users', function(data){
this.$set('users', data);
});
}
}
</script>
Run Code Online (Sandbox Code Playgroud) 我想从Node.js生成客户端JavaScript.我找到了Browserify,但除了Browserify还有其他选择吗?
嗨,我在使用browserify时遇到了问题,我做了browserify main.js > bundle.js所有工作,直到遇到功能为止。现在我得到:
Uncaught TypeError: search is not a function
at HTMLButtonElement.onclick (index.html:30)
Run Code Online (Sandbox Code Playgroud)
这是我的main.js上的内容:
function search() {
[..] trigger stuff
}
Run Code Online (Sandbox Code Playgroud)
在我的html文件中,我有:
<input type="text" id="search" placeholder="Search">
<button class="ui button" onclick="search()">
Run Code Online (Sandbox Code Playgroud)
其他只是设计和标题。
伙计们。我习惯在 NodeJS 中通过 require('library') 加载库。现在我负责一个非常重要的项目的前端,希望它能给我的生活带来我应得的幸福。
问题是没有人能够提供一种简单、随时可用的模块加载解决方案。
Webpack:对于开发来说不可靠。需要创建配置文件并阅读一堆东西,没有一个有好工作的人有时间这样做。糟糕的文档,看起来编辑文件时会给我带来困难(我在一个屏幕上有浏览器,在另一个屏幕上有文本编辑器,所以我编辑一行并按 f5 立即看到结果,似乎并不无需在 webpack 中进行额外的努力即可实现)
Browserify/RequireJS 都差不多,比较麻烦
为什么没有人能够在浏览器中复制 NodeJS 的“require”系统?我只想做 require('library') 并忘记它。没有未经请求的配置文件。无需额外工具。不说废话。只是。
有当我们写这两个有什么区别HTML和JavaScript代码在一个单一的.html文件或写HTML代码放在一个.html文件,并JavaScript在一个.js单独的文件?我的意思是这两种情况下的功能是一样的吗?
前任。在这里:https://github.com/tomconte/solarchain-dashboard HTML和JavaScript写在两个独立的.html和.js文件,并在上面.js的文件中提到:注:需要编译browserify viz.js -o main.js。
我的意思是如果文件中的JavaScript代码viz.js写在index.html文件中,我们还需要使用browserify来编译JavaScript代码吗?
index.html文件:https : //github.com/tomconte/solarchain-dashboard/blob/master/index.html
viz.js文件:https : //github.com/tomconte/solarchain-dashboard/blob/master/viz.js
browserify ×9
javascript ×6
node.js ×4
html ×2
coffeescript ×1
compilation ×1
ecmascript-6 ×1
fs ×1
reactjs ×1
requirejs ×1
vue.js ×1
webpack ×1
websocket ×1