【发布时间】:2019-12-02 03:35:34
【问题描述】:
所以我最近只使用 laravel 框架完成了我的项目。现在我已经完成了它的工作,我想通过刷新内容而不刷新布局页面来将 vue.js 添加到我的项目中。而且我想将我的刀片文件转换为 vue 组件。而且我不知道该怎么做,因为在我的项目的每个部分中,我都有 4 个刀片文件,如索引、编辑、创建、显示,我不知道如何在组件中制作它,而且很难我是因为我使用的是 laravel 集体形式,这就是为什么每次我在数据库中添加一些条目时它都会刷新。我也是 vuejs 的新手。有人可以帮我解决这个问题吗?非常感谢。
我的文件夹目录是这样的。
-roadmap
---index.blade.php
---show.blade.php
---edit.blade.php
---create.blade.php
这是我的一些代码。
路线图/index.blade.php
@extends('layouts.admin')
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- DATA TABLES -->
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet"href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<div><a class="btn btn-success" style="float:right" href="{{ route('roadmap.create') }}">Add Roadmap</a></div>
<table id="myTable" class="table table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Year Covered </th>
<th scope="col">Description</th>
<th scope="col">Date entered</th>
<th width="280px">Action</th>
</tr>
</thead>
<tbody>
@foreach ($roadmap as $data)
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->year}}</td>
<td>{{ $data->body}}</td>
<td>{{ $data->created_at}}</td>
<td>
<a href="/roadmap/{{$data->id}}/edit" class="btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a>
<a href="/roadmap/{{$data->id}}" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span></a>
{!! Form::open(['method' => 'DELETE', 'route'=>['roadmap.destroy', $data->id], 'style'=> 'display:inline', 'onsubmit' => 'return confirm("Are you sure you want to delete?")']) !!}
{!! Form::button('<i class="fa fa-trash"></i>',['type'=>'submit', 'class'=> 'btn btn-danger']) !!}
{!! Form::close() !!}</td>
</tr>
@endforeach
</tbody>
</table>
<script>
$(document).ready(function() {
$('#myTable').DataTable();
} );
</script>
@endsection
RoadmapController.php
<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\Roadmap;
use Validator;
use Illuminate\Foundation\Validation\ValidatesRequests;
class RoadmapController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$roadmap = DB::table('roadmaps')->get();
return view('roadmap.index', ['roadmap' => $roadmap]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('roadmap.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
request()->validate([
'year' =>['required', 'string', 'max:255', 'unique:roadmaps'],
'body' => ['required', 'string', 'max:255'],
]);
Roadmap::create($request->all());
return redirect()->route('roadmap.index')->with('success','Created successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$roadmap = Roadmap::find($id);
return view('roadmap.show', compact('roadmap'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
$roadmap = Roadmap::find($id);
return view('roadmap.edit', compact('roadmap'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
request()->validate([
'year' => 'required',
'body' => 'required',
]);
Roadmap::find($id)->update($request->all());
return redirect()->route('roadmap.index')->with('success',' Updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
Roadmap::find($id)->delete();
return redirect()->route('roadmap.index')->with('success','News deleted successfully');
}
}
web.php
//CRUD COLLECTIVE ROADMAP
Route::resource('roadmap', 'RoadmapController');
【问题讨论】:
-
你知道刀片组件的使用方法吗?
-
我知道一点先生,但我很困惑是否应该在 vue 中创建 4 个组件,如 create、index 等。我只知道如何通过创建
<component-name>在刀片文件中渲染我的组件和一些小的 CRUD axios。我也很困惑@foreach是否可以在我的组件中工作,因为我使用控制器中的compact函数将数据传递给我的视图
标签: javascript php laravel vue.js