【问题标题】:How to update v-model data in laravel blade file in edit form如何以编辑形式更新 laravel 刀片文件中的 v-model 数据
【发布时间】:2019-11-06 10:08:27
【问题描述】:

我有一个用于创建和更新类别数据的文件。

在创建类别时,我在标题中使用v-model 也以相同的形式创建 slug。

创建时效果很好,但我面临更新/编辑表单部分的问题。

下面是我的代码:


<input class="form-input mt-1 block w-full" name="name" v-model="title" v-on:keyup="getSlug" placeholder="Category Title" value="{{ $category->name ?? '' }}">

添加整个刀片文件代码以供参考:

@extends('layouts.backend.app')

@php
if(isset($record) && $record != null){
    $edit = 1;
} else {
    $edit = 0;
}
@endphp

@section('content')

    <section id="app" class="container mx-auto">

        <div class="flex items-center justify-between">
            <h1 class="text-2xl font-bold">
            @if($edit) Edit @else Create @endif Category Form
            </h1>
            <a href="{{route('category.index')}}" class="border rounded px-4 py-2">Back</a>
        </div>

        <div class="border rounded mt-8 p-8">
            <form action="@if($edit) {{route('category.update', $record->id)}}  @else {{route('category.store')}}  @endif" method="POST">
                @csrf
                <label class="block mb-4">
                    <span class="text-gray-700 font-bold">Name</span>
                    <input class="form-input mt-1 block w-full" name="name" v-model="title" v-on:keyup="getSlug" placeholder="Category Title" value="{{ $category->name ?? '' }}">
                    @error('name')
                        <span class=" text-red-400 invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </label>

                <label class="block mb-4">
                    <span class="text-gray-700 font-bold">Slug</span>
                    <input 
                        class="form-input mt-1 block w-full" 
                        name="slug" 
                        id="slug" 
                        v-model="slug" 
                        placeholder="Slug / Pretty URL">
                    @error('slug')
                        <span class=" text-red-400 invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </label>

                <label class="block mb-8">
                    <span class="text-gray-700 font-bold">Banner</span>
                    <input 
                        class="form-input mt-1 block w-full" 
                        name="banner" 
                        value="@if($edit) {{$record->banner}} @else https://source.unsplash.com/random  @endif"
                        placeholder="Banner / URL">
                </label>

                <div class="flex justify-between">
                    <button class="px-4 py-2 border bg-gray-200 text-gray-900 rounded" type="submit">Submit</button>
                    <button class="px-4 py-2 border bg-red-400 text-white rounded" type="rest">Reset</button>
                </div>

            </form>
        </div>

        </section>

@endsection

如您所见,标题输入用于在 keyup 事件上创建 slug。现在给定的代码不使用数据库中的数据来预填充编辑表单标题输入字段。因为我使用的是v-model="title",它在我的 app.js 中,目前为空。

如何从database 分配v-model="title" 我的当前值。

这不是vue 组件。它是一个laravel blade 文件。请为此指导我。

谢谢

【问题讨论】:

    标签: laravel forms vuejs2 edit


    【解决方案1】:

    您可以将 app.js 代码移动到 Blade 视图并使用 Blade 表达式填充 title

    @extends('layouts.backend.app')
    
    @php
    if(isset($record) && $record != null){
    $edit = 1;
    } else {
    $edit = 0;
    }
    @endphp
    
    @section('content')
    <section id="app" class="container mx-auto">
        <div class="flex items-center justify-between">
            <h1 class="text-2xl font-bold">
                @if($edit) Edit @else Create @endif Category Form
            </h1>
            <a href="{{route('category.index')}}" class="border rounded px-4 py-2">Back</a>
        </div>
        <div class="border rounded mt-8 p-8">
            <form action="@if($edit) {{route('category.update', $record->id)}}  @else {{route('category.store')}}  @endif"
                method="POST">
                @csrf
                <label class="block mb-4">
                    <span class="text-gray-700 font-bold">Name</span>
                    <input class="form-input mt-1 block w-full" name="name" v-model="title" v-on:keyup="getSlug"
                        placeholder="Category Title" value="{{ $category->name ?? '' }}">
                    @error('name')
                    <span class=" text-red-400 invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </label>
                <label class="block mb-4">
                    <span class="text-gray-700 font-bold">Slug</span>
                    <input class="form-input mt-1 block w-full" name="slug" id="slug" v-model="slug"
                        placeholder="Slug / Pretty URL">
                    @error('slug')
                    <span class=" text-red-400 invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </label>
                <label class="block mb-8">
                    <span class="text-gray-700 font-bold">Banner</span>
                    <input class="form-input mt-1 block w-full" name="banner"
                        value="@if($edit) {{$record->banner}} @else https://source.unsplash.com/random  @endif"
                        placeholder="Banner / URL">
                </label>
                <div class="flex justify-between">
                    <button class="px-4 py-2 border bg-gray-200 text-gray-900 rounded" type="submit">Submit</button>
                    <button class="px-4 py-2 border bg-red-400 text-white rounded" type="rest">Reset</button>
                </div>
            </form>
        </div>
    </section>
    {{-- Place the script here, after closing the section with id of app --}}
    <script>
        const app = new Vue({
            el: '#app',
            data() {
                return {
                    title: '{{ $category->name }}'
                }
            },
            methods: {
                getSlug() {
                    this.title = this.title.toLowerCase()
                    .replace(/ /g,'-')
                    .replace(/[^\w-]+/g,'');
                }
            }
        });
    </script>
    @endsection
    

    希望对你有帮助

    【讨论】:

    • [Vue 警告]:编译模板时出错:模板应该只负责将状态映射到 UI。避免在模板中放置带有副作用的标签,例如
    • 关闭脚本标签前的div id="app",请编辑问题并发布您的刀片视图,一个输入是不够的,因为您让我们猜测
    • 好的,更新了答案,确保您在其他任何地方都没有其他具有 app id 的元素
    • 这就是问题所在。 app.js 是整个应用的父控件。我总是可以使用其他关键字,但问题是现在我得到了:[Vue 警告]:编译模板时出错:模板应该只负责将状态映射到 UI。避免在模板中放置带有副作用的标签,例如
    • 你不能在同一页面中多次使用相同的 id "app",编辑问题并显示父视图,我认为你可以生成脚本以避免此错误
    【解决方案2】:

    最后,我又回到了旧的做事方式。

    代码如下:

    
    getSlug() {
            var title = document.getElementById('title').value;
            document.getElementById('slug').value = title.replace(/\s+/g, '-').toLowerCase().trim();
          },
    
    

    并将输入字段上的v-model 替换为id 标签。

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2022-01-26
      • 2023-03-26
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2020-01-23
      相关资源
      最近更新 更多