javascript
<template>
<div>{{ displayString }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
displayString: String
});
</script>
<template>
<div>{{ displayString }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
displayString: String
});
</script>
<template>
<p v-if="condition">Condition is true</p>
<p v-else>Condition is false</p>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
condition: Boolean
});
</script>
<template>
<intermediate-component :data="data" />
</template>
<script setup>
import { defineProps } from 'vue';
import IntermediateComponent from './IntermediateComponent.vue';
const props = defineProps({
data: String
});
</script>
<template>
<button @click="setClicked">
{{ clicked ? "Button clicked" : "Click me" }}
</button>
</template>
<script setup>
import { ref } from 'vue';
const clicked = ref(false);
function setClicked() {
clicked.value = true;
}
</script>
<template>
<input
type="text"
v-model="text"
placeholder="Enter text"
/>
</template>
<script setup>
import { ref } from 'vue';
const text = ref('');
</script>
Vue.js doesn't have a built-in preview feature. However, you can use a tool like Storybook to create previews for your components in a separate development environment.
<template>
<ul>
<li v-for="item in items" :key="item">
{{ item }}
</li>
</ul>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
items: Array
});
</script>
<!-- Usage -->
<list-component :items="['Item 1', 'Item 2', 'Item 3']"></list-component>
<template>
<ul>
<li v-for="person in items" :key="person.id">
Name: {{ person.name }}, Age: {{ person.age }}
</li>
</ul>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
items: Array
});
</script>
<!-- Usage -->
<item-keys-example
:items="[
{ name: 'John', age: 30, id: '1' },
{ name: 'Jane', age: 28, id: '2' },
{ name: 'Bob', age: 25, id: '3' }
]"
></item-keys-example>
// ParentComponent.vue
<template>
<div>
<slot name="header"></slot>
<slot name="content"></slot>
</div>
</template>
// ChildComponent.vue
<template>
<p>Child Content</p>
</template>
// Usage
<parent-component>
<template v-slot:header>
<h1>Header</h1>
</template>
<template v-slot:content>
<child />
</template>
</parent-component>
Vue.js doesn't have a direct analog to modifiers in Jetpack Compose or SwiftUI. Instead, you can use inline styles or CSS classes.
<template>
<div :style="style">Hello, World!</div>
</template>
<script setup>
import { reactive } from 'vue';
const style = reactive({
padding: '16px',
backgroundColor: 'blue',
color: 'white'
});
</script>
<template>
<button @click="incrementCount">
Count: {{ count }}
</button>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
function incrementCount() {
count.value++;
}
</script>
<!-- ParentComponent.vue -->
<template>
<intermediate />
</template>
<script setup>
import { provide, ref } from 'vue';
import Intermediate from './IntermediateComponent.vue';
const data = ref('Some data');
provide('dataKey', data);
</script>
<!-- IntermediateComponent.vue -->
<template>
<child />
</template>
<script setup>
import Child from './ChildComponent.vue';
</script>
<!-- ChildComponent.vue -->
<template>
<p>Received data: {{ data }}</p>
</template>
<script setup>
import { inject } from 'vue';
const data = inject('dataKey');
</script>
<!-- Usage -->
<parent-component data="Some data"></parent-component>
<template>
<div></div>
</template>
<script setup>
import { onMounted } from 'vue';
onMounted(() => {
// Perform side effect here
});
</script>