我需要将值传递给数据中的样式属性,但是,由于 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 的语法是什么?
谢谢!