错误:
错误:未捕获(在承诺中):错误:StaticInjectorError [AddItemPage]: StaticInjectorError [AddItemPage]: NullInjectorError:没有AddItemPage的提供者!错误:StaticInjectorError [AddItemPage]:StaticInjectorError [AddItemPage]:
NullInjectorError: No provider for AddItemPage! at _NullInjector.get (http://localhost:8100/build/vendor.js:1276:19) at resolveToken (http://localhost:8100/build/vendor.js:1564:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1506:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1377:20) at resolveToken (http://localhost:8100/build/vendor.js:1564:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1506:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1377:20) at resolveNgModuleDep (http://localhost:8100/build/vendor.js:10938:25) at NgModuleRef_.get (http://localhost:8100/build/vendor.js:12159:16) at resolveNgModuleDep (http://localhost:8100/build/vendor.js:10938:25) at c (http://localhost:8100/build/polyfills.js:3:19752) at Object.reject (http://localhost:8100/build/polyfills.js:3:19174) at NavControllerBase._fireError (http://localhost:8100/build/vendor.js:48310:16) at NavControllerBase._failed (http://localhost:8100/build/vendor.js:48303:14) at http://localhost:8100/build/vendor.js:48350:59 at t.invoke (http://localhost:8100/build/polyfills.js:3:14976) at Object.onInvoke (http://localhost:8100/build/vendor.js:4982:33) at t.invoke (http://localhost:8100/build/polyfills.js:3:14916) at r.run (http://localhost:8100/build/polyfills.js:3:10143) at http://localhost:8100/build/polyfills.js:3:20242
当我在HomePage中写道时:
import { AddItemPage } from './../add-item/add-item';
...
constructor(... ,private addItemPage: AddItemPage){...}
...
editItems(item){
this.addItemPage.editItem(item);
}
所以我想从主页中的 AddItemPage 调用方法。 任何解决方案?
答案 0 :(得分:0)
将导入添加到 app.module.ts
...
import { AddItemPage } from '../add-item/add-item';
@NgModule({
declarations: [
MyApp,
HomePage,
AddItemPage
],
...
答案 1 :(得分:0)
您正在像提供商一样调用AddItemPage。因此,如果AddItemPage是一个页面,则需要将其导入app.module.ts声明数组。 然后,您想从HomePage中的AddItemPage调用方法。要访问组件及其方法,可以使用@ViewChild装饰器。
'use strict';
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
var Kafka = require('no-kafka');
var bodyParser = require('body-parser');
let techIds = [];
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', function (req, res) {
res.send("hello.");
});
//Socket IO Method
io.on('connection', (socket) => {
console.log('USER CONNECTED');
socket.on('join', function (data) {
clients.push(
{
'socketId': socket.id,
'mgrId': data.mgrId,
'techIds': data.attuIds
});
//console.log(socket.id + ' ' + data.mgrId + ' USER CONNECTED!!');
});
socket.on('disconnect', function (data) {
if (clients.length > 0) {
let item = clients.find(x => x.socketId == socket.id);
const index = clients.indexOf(item);
if (index > -1) {
//console.log(clients[index].mgrId + ' USER DISCONNECTED!!');
clients.splice(index, 1);
// console.log(clients);
}
}
});
});
http.listen(3007, () => {
console.log('started on port 3007');
var consumer = new Kafka.SimpleConsumer({
connectionString: 'localhost:29092,localhost:29093,localhost:29094',
clientId: 'no-kafka-client'
});
var dataHandler = function (messageSet, topic, partition) {
messageSet.forEach((m) => {
console.log(topic, partition, m.offset, m.message.value.toString('utf8'));
if(topic=="MyMessage")
{
const msg = JSON.parse(m.message.value.toString('utf8'));
if (clients.length > 0) {
for (var index = 0; index < clients.length; index++) {
var ids = clients[index].techIds;
var idx = ids.indexOf(msg.techID.toLowerCase());
if (idx > -1) {
if (io.sockets.connected[clients[index].socketId]) {
io.sockets.connected[clients[index].socketId].emit('message', msg);
}
}
}
}
}
});
}.bind(this);
return consumer.init().then(function () {
var v1= consumer.subscribe('MyMessage', [0, 1], dataHandler);
var arr=[];
arr.push([v1]);
return arr;
});
});
我不知道目标是什么,但我认为你可以采用不同的方式。 希望我能帮忙!