laravel-5 - 使用 Laravel 和 Vue 实现前端的最佳实践

标签 laravel-5 vue.js frontend vuejs2

我正在开发一个基于 Laravel 5.4 的大型应用程序。我想知道实现大规模应用程序前端的最佳实践是什么?在 laravel Blade 上实现所有样式和 html 渲染并使用 vue 进行交互或使用blade 调用 vue 组件并实现所有内容是 vue?让我们看一些例子:

这是第一种方法:

在 laravel Blade 中:

@extends('layout')
@section('content')
  <customer-search></customer-search>
  <customer-table></customer-table>
@endsection

那么客户搜索组件将是:

<template>
  <div>
    <form action="/customer" method="GET">
      <input type="text" name="name" @input="updateValue($event.target.value)" :value="name" />
      <submit @click="search">Search</button>
    </form>
  </div>
</template>
<script>
  export default {
    data: function () {
      return {
        name: '',
      }
    },
    methods: {
      search() {
        // Get data from server, update related data model for using in customer table, ...
      }
    }
  }
</script>

和客户表组件:

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Access</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="customer in customers">
          <td>{{ customer.name }}</td>
          <td><a href="#">Link</a></td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script>
  export default {

  }
</script>

第二种方法: Blade :

@extends('layout')
@section('content')
  <customer-index>
    <form action="/customer" method="GET">
      <input type="text" name="name" @input="updateValue($event.target.value)" :value="name" />
      <submit @click="search">Search</button>
    </form>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Access</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="customer in customers">
          <td>{{ customer.name }}</td>
          <td><a href="#">Link</a></td>
        </tr>
      </tbody>
    </table>
  </customer-index>
@endsection

和客户索引组件:

<tempalte>
  <div>
    <slot></slot>
  </div>
</template>
<script>
  export default {
    data: function () {
      return {
        name: '',
      }
    },
    methods: {
      search() {
        // Get data from server, update related data model for using in customer table, ...
      },
      // Other methods, ...
    }
  }
</script>

第三种可能性: 第三种可能性是尝试使用第二种方法并更深入地了解码件。例如,使用表格组件、表单组件、输入组件、按钮组件…… 我应该使用哪一个来热衷于在前端花费大量时间并且还拥有集成的前端?

最佳答案

Vue 组件应该始终是松散耦合的,这样它们就可以在其他地方甚至其他项目中重用。

您应该在 vue 组件中使用尽可能少的标记,以便它们可移植并且可以重用,而无需您编辑它们。

显然这是一个基于意见的问题,但至少在我看来,以上是最佳实践。

关于laravel-5 - 使用 Laravel 和 Vue 实现前端的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43090539/

相关文章:

php - 播种前无法截断表

reactjs - Laravel 5.4 无法运行 "php artisan preset react"命令

php - 将excel文件导入数据库时​​超时

http - 如何在 vue-cli 中设置 HTTP header ?

vue.js - 如何在嵌套对象中搜索

rest - 如何使用隔离的前端托管多服务解决方案

laravel - 如何从不是 Controller 方法的方法发送响应?

javascript - Vue 路由器 : hide parent template when child is open

javascript - 在自定义指令的链接函数中访问已编译的 ng-repeat 子元素

javascript - React.js 使用 JQuery 发布 Django 得到 403