Skip to content

Instantly share code, notes, and snippets.

@Raserad
Last active February 7, 2022 06:38
Show Gist options
  • Save Raserad/df6813895c1aa413f0a158b097011a95 to your computer and use it in GitHub Desktop.
Save Raserad/df6813895c1aa413f0a158b097011a95 to your computer and use it in GitHub Desktop.
0.0.1
//First variant ---------------------------------------------------------------
<template>
<div class="some__component" @click="doSmth" :style="styles">
<img
class="some__component_content"
:src="url"
/>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { getParentHeight } from './helpers';
export default defineComponent({
props: {
url: {
type: String,
required: true
}
},
data() {
return {
height: 10,
}
},
methods: {
doSmth() {
// Вот это использую метод напрямую
this.height = getParentHeight()
}
},
computed: {
styles(): Object {
return {
height: this.height,
}
}
}
})
</script>
<style>
.some__component {
display: flex;
}
.some__component_content {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
//Second variant -------------------------------------------------------------------------
<template>
<div class="some__component" @click="doSmth" :style="styles">
<img
class="some__component_content"
:src="url"
/>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { getParentHeight } from './helpers';
export default defineComponent({
props: {
url: {
type: String,
required: true
}
},
data() {
return {
height: 10,
}
},
setup() {
return {
getParentHeight
}
},
methods: {
doSmth() {
//А вот здесь сначала передаю в setup, затем использую через this
this.height = this.getParentHeight()
}
},
computed: {
styles(): Object {
return {
height: this.height,
}
}
}
})
</script>
<style>
.some__component {
display: flex;
}
.some__component_content {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment