The re-expressed title is: Execution of mounted hook encountered an unhandled error
P粉940538947
P粉940538947 2023-12-18 21:47:29
0
1
691

I'm creating a to-do web application. I have successfully fetched all to-do list items and displayed them on my homepage. The problem comes when I try to get just one article and display it, it returns hook is not a function error.

I use laravel on the server side and vite on the client side.

The following is my api.php route

<?php

use AppHttpControllersTodoController;
use AppHttpControllersTodosController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});


Route::get('/todos', TodosController::class);
Route::get('/todos/{todo:uuid}', TodoController::class);

In the third route, I use uuid to get a single post and display it. Displays

correctly on the server side

The following are my TodoController.php and TodoResource.

<?php

namespace AppHttpControllers;

use AppHttpResourcesTodoResource;
use AppModelsTodo;
use IlluminateHttpRequest;

class TodoController extends Controller
{
    //
    public function __invoke(Todo $todo)
    {
        return new TodoResource($todo);
    }
}

TodoResource.php

<?php

namespace AppHttpResources;

use IlluminateHttpResourcesJsonJsonResource;

class TodoResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return array|IlluminateContractsSupportArrayable|JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'uuid' => $this->uuid,
            'name' => $this->name,
            'completed' => $this->completed,
            'completed_at' => $this->completed_at,
        ];
    }
}

I have fetched the post on the client server side using axios. The following is the code of useTodos.js

import { ref } from 'vue'

import axios from 'axios'


export default function useTodos() {
    const todos = ref([])
    const todo = ref([])


    const fetchTodos = async () => {
        let response = await axios.get('/api/todos')
        todos.value = response.data.data
    }


    const fetchTodo = async (uuid) => {
        let response = await axios.get(`/api/todos/${uuid}`)
        console.log(response)
        todo.value = response.data.data
    }


    return {
        todos,
        fetchTodos,
        todo,
        fetchTodo
    }
}

The following is a page that displays a single post on the client-server side

<template>
    <div>
        {{ todo }}
    </div>
</template>

<script>

import useTodos from '../api/useTodos'
import { onMounted } from 'vue'

export default {
    props: {
        uuid: {
            required: true,
            type: String
        }
    },
    setup(props) {
        
        const { todo, fetchTodo } = useTodos()

        onMounted(fetchTodo(props.uuid))

        return {
            todo
        }
    },
}
</script>

My Router Page

import { createRouter, createWebHistory } from 'vue-router'

import Home from '../pages/Home.vue'
import Todo from '../pages/Todo.vue'

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/todos/:uuid',
        name: 'todo',
        component: Todo,
        props: true
    }
]

export default createRouter({
    history: createWebHistory(),
    routes
})

I'm trying to find a solution to why when I try to get a single post, the hook it brings is not a function. Can anyone help me solve this problem?

P粉940538947
P粉940538947

reply all(1)
P粉897881626

Try running it this way

async onMounted(() => {
  await fetchTodo(props.uuid)
})

As shown below: https://vuejs.org/api/composition -api-lifecycle.html#onmounted

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!