axios [Vue warn]:执行已挂载挂接时出现未处理的错误

iaqfqrcu  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(236)

我正在创建一个待办网页应用程序。我已经成功地获取了所有待办事项列表项并将其显示在我的主页上。当我试图只获取一个帖子并显示它时,问题出现了,它返回hook不是函数错误。
我在服务器端使用laravel,在客户端使用vite。
下面是我的api.php路径

<?php

use App\Http\Controllers\TodoController;
use App\Http\Controllers\TodosController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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);

在第三个路由中,我使用uuid获取单个post并显示它,它在服务器端正确显示
下面分别是我的TodoController.php和TodoResource。

<?php

namespace App\Http\Controllers;

use App\Http\Resources\TodoResource;
use App\Models\Todo;
use Illuminate\Http\Request;

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

TodoResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

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

我已经用axios在客户端和服务器端获取了这个帖子。

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
    }
}

下面是在客户端-服务器端显示单个帖子的页面

<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>

我的路由器页面

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
})

我试图找到解决办法,为什么它带来的挂钩是不是一个功能,当我试图获取单个职位。有人能帮助我在这个问题上?

dgenwo3n

dgenwo3n1#

试着这样做

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

如图所示:https://vuejs.org/api/composition-api-lifecycle.html#onmounted

相关问题