如何在Vue中获得元素调用函数(无事件)

时间:2018-11-28 11:48:48

标签: vue.js

我有这段代码,我需要在getWidth函数上有一个调用div。这可能吗?由于这不是事件,所以我不确定如何处理。通常我会做$event,但在这种情况下不存在。

<div :style="{width: getWidth($this_element)}">
</div>

这包含在v-for循环中。

1 个答案:

答案 0 :(得分:0)

当前的DOM元素已通过this.$elimport { TestBed, async, inject } from '@angular/core/testing'; import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { AdapterService } from './adapter.service'; describe('AdapterService', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [ HttpClientModule, HttpClientTestingModule ], providers: [ AdapterService ] }); }); afterEach(inject([HttpTestingController], (backend: HttpTestingController) => { backend.verify(); })); it('should send a valid get request for token', async(inject([AdapterService, HttpTestingController], (service: AdapterService, backend: HttpTestingController) => { service.get('oauth2/token').subscribe((next)=>{ expect(next).toBeDefined(); }); }))); // it('') }); 中可用。如果要在JavaScript中访问元素的宽度,可以执行以下操作:

getWidth()

<template>
  <div :style="{width: getWidth()}">...</div>
</template>

<script>
export default {
  methods: {
    getWidth() {
      return (this.$el.clientWidth * .33) + 'px';
    }
  }
}
</script>
new Vue({
  el: '#app',
  methods: {
    getWidth() {
      return (this.$el.clientWidth * .33) + 'px';
    }
  }
})
.my-el {
  display: inline-block;
  background: #eee;
}