1👍
Using a setup function
<script setup>
if (process.server) {
const event = useRequestEvent()
setResponseStatus(event, 404)
}
</script>
Since it only works on server you can wrap it in process.server
.
Using (inline) route middleware
<script setup>
definePageMeta({
middleware: [
() => {
const event = useRequestEvent()
setResponseStatus(event, 404)
}
],
});
</script>
Note that you need a route middleware, not a server middleware. Server middleware run on every request which is probably not what you want.
For the sake of simplicity I made the middleware inline but you can have it in the middleware directory.
- [Vuejs]-How do you target a button, which is in another component, with a method in App.vue?
- [Vuejs]-Example works in js-fiddle but not in self-made page
Source:stackexchange.com