我试图从Firebase获取数据并将其设置为Class中的变量.我面临的问题是ngAfterViewInit()中的变量未定义.
我知道它是在ngOnInit()中设置的.我已经在Angular文档页面中阅读了Angular生命周期方法和英雄之旅示例,看看我是否正确获取数据,但它仍然无法解释这种奇怪的行为(至少对我而言!).
ngOnInit() {
this.http.get('https://xxxxxxxx-xxxxx.firebaseio.com/data.json').map(response => response.json())
.subscribe(
data => {
for (var i = 0; i < data.length; i++) {
var a: string = data[i]['date'];
data[i]['date'] = new Date(a);
}
this.timeline = data; // declared in class
this.timelineElements = data; // declared in class
console.log(data); // data is not empty
console.log(TIMELINE); // data is not empty
},
error => {
console.log(error);
}
)}
ngAfterViewInit() {
console.log(this.timeline); // Undefined if fetched from firebase. Works perfectly if the data is fetched …
Run Code Online (Sandbox Code Playgroud) 这些是webpack.config.js文件的内容
var webpack = require('webpack');
var path = require('path');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
module.exports = {
entry: [
'script!jquery/dist/jquery.min.js',
'script!foundation-sites/dist/foundation.min.js',
'./app/app.jsx'
],
externals:{
jquery: 'jQuery'
},
plugins:[
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery'
}),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
})
],
output: {
path: __dirname,
filename: './public/bundle.js'
},
resolve:{
root: __dirname,
modulesDirectories: [
'node_modules',
'./app/components',
'./app/api'
],
alias: {
app: 'app',
applicationStyles:'app/styles/app.scss',
actions:'app/actions/actions.jsx',
reducers:'app/reducers/reducers.jsx',
configureStore:'app/store/configureStore.jsx'
},
extensions: ['','.js',['.jsx']]
},
module:{
loaders: [
{
loader: 'babel-loader',
query: { …
Run Code Online (Sandbox Code Playgroud)