我有AddressController,在这个控制器中,我有一个函数loadCity和其他CRUD函数,如下所示:

public function loadCities(Request $request)
    {
            $provinceId = $request->province_id;
            $cities = Province::where('province_id', $provinceId)->get();

            return response()->json($cities);
    }

我的create.vue脚本:

<Multiselect v-model="form.province_id" :options="provinces" valueProp="id" label="name" trackBy="name" :searchable="true" placeholder="Select:" @change="loadCities" />

const cities = ref([]); 

const loadCities = async () => {
    try {
        const response = await router.get(`/apps/address/load-cities/${form.province_id}`);
        cities.value = response.data;
    } catch (error) {
        console.error(error);
    }
}

这是我的路由web.id:

Route::resource('/address', AddressController::class, ['as' => 'apps'])
    ->middleware('permission:address.index|address.create|address.edit|address.delete')
    ->except(['show']);

        Route::get('/address/load-cities/{province_id}', [AddressController::class, 'loadCities'], ['as' => 'apps'])
        ->name('apps.address.loadCities');

但现在的问题是错误:

The GET method is not supported for this route. Supported methods: PUT, PATCH, DELETE.

我可以像这样使用intertia路由创建依赖的路由吗?或者我的代码缺少了什么?之前谢谢你.

最新情况: 如果我sole.log(form.spectra_id),它返回NULL.

推荐答案

在路由文件中,使用get方法定义loadCities路由:

 Route::get('/address/load-cities/{province_id}', [AddressController::class, 'loadCities'])->name('apps.address.loadCities');

在create.vue脚本中,更新loadCities方法以向正确的路由发出GET请求:

const loadCities = async () => {
    try {
        const response = await router.get(`/address/load-cities/${form.province_id}`); // Update the route
        cities.value = response.data;
    } catch (error) {
        console.error(error);
    }
}

您可以确保只有在 Select 了PROMENT_ID之后才调用loadCities函数.您可以通过监听多选组件上的输入事件来实现这一点.

<Multiselect v-model="form.province_id" :options="provinces" valueProp="id" label="name" trackBy="name" :searchable="true" placeholder="Select:" @input="handleProvinceChange" />

然后,在Vue组件中定义review ProvinceChange方法:

const handleProvinceChange = async () => {
    // Check if the province_id is not null or undefined
    if (form.province_id) {
        await loadCities(); // Call the loadCities function
    }
}

确保loadCities方法定义在AddressProvinceChange方法之上,以避免任何引用问题.

在Vue组件中,为form. province_id属性创建一个watcher:

watch: {
  'form.province_id': {
    immediate: true, // Call the handler immediately after the component is created
    handler(newProvinceId, oldProvinceId) {
      if (newProvinceId) {
        loadCities(); // Call the loadCities function when the province_id changes
      }
    }
  }
}

Javascript相关问答推荐

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

Plotly热图:在矩形上zoom 后将zoom 区域居中

我可以从React中的出口从主布局调用一个处理程序函数吗?

配置WebAssembly/Emscripten本地生成问题

构造HTML表单以使用表单数据创建对象数组

如何解决useState错误—setSelect Image不是函数''

将核心模块导入另一个组件模块时存在多个主题

在HTML语言中调用外部JavaScript文件中的函数

如何在Svelte中从一个codec函数中调用error()?

使用插件构建包含chart.js提供程序的Angular 库?

MongoDB受困于太多的数据

如何使用画布在另一个内部绘制一个较小但相同的形状,同时保持恒定的边界厚度?

用于测试其方法和构造函数的导出/导入类

P5JS-绘制不重叠的圆

不协调嵌入图片

无法使用Redux异步函数读取未定义的useEffect钩子的属性';map';

我们是否可以在reactjs中创建多个同名的路由

扩散运算符未按预期工作,引发语法错误

当S点击按钮时,我如何才能改变它的样式?

JSON Web令牌(JWT)错误:RSA密钥对的签名无效