データ内のスタイル プロパティに値を渡す必要がありますが、vuejs と JS スコープの仕組みが原因で、this.data.property からアクセスできません。
Vue.component ('loader-component', {
template: '#loader-template',
mounted: function() {
this.animationTest();
},
data: function() {
return {
svg: true,
timer: 0,
// styles
position: {
marginLeft: '',
marginTop: '',
float: 'left'
}
};
},
methods: {
animation: function() {
let timer = 0,
itemWidth = 60,
domWidth = document.getElementById('awesome-body').clientWidth,
domHeight = document.getElementById('awesome-body').clientHeight,
marginL = -2 * itemWidth,
marginT = Math.floor((Math.random() * domHeight) + 1);
this.position.marginTop = marginT;
setInterval(function() {
marginL = marginL + timer * 5;
timer++;
// console.log(marginL);
this.position.marginLeft = marginL;
}, 1000); // time interval in milliseconds
}
} // methods finishes
});
これにより、次のエラーがトリガーされます。
Cannot set property 'marginLeft' of undefined.
setInterval 関数から data.marginTop に直接移動する構文は何ですか?
ありがとう!