<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
// State management
const count = ref(0);
// Methods
const increment = () => {
count.value++;
};
// Return the state and methods to the template
return {
count,
increment
};
}
};
</script>
<template>
<div id="app">
<Counter />
</div>
</template>
<script>
import Counter from './components/Counter.vue';
export default {
components: {
Counter
}
};
</script>
<template>
<div>
<p v-if="loading">Loading...</p>
<p v-else-if="error">{{ error }}</p>
<div v-else>
<h3>{{ data.title }}</h3>
<p>{{ data.body }}</p>
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue';
export default {
setup() {
const data = ref(null);
const loading = ref(true);
const error = ref(null);
const fetchData = async () => {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const result = await response.json();
data.value = result;
} catch (err) {
error.value = 'Failed to fetch data';
} finally {
loading.value = false;
}
};
onMounted(fetchData);
return {
data,
loading,
error
};
}
};
</script>
<template>
<div id="app">
<DataFetcher />
</div>
</template>
<script>
import DataFetcher from './components/DataFetcher.vue';
export default {
components: {
DataFetcher
}
};
</script>