arrilot/vue-templates-in-php

一个简单的包,用于管理 PHP 中的 Vue 组件模板

0.1.3 2018-10-10 18:25 UTC

This package is auto-updated.

Last update: 2024-09-21 00:54:39 UTC


README

Latest Stable Version Total Downloads Build Status

PHP 中的 Vue 模板

介绍

处理 Vue 组件模板主要有两种方法

  1. 单文件组件
  2. <script type="text/x-template">

单文件组件很棒,但它们也有自己的问题

  1. 你需要一个合理的构建设置。
  2. 你不能直接从 PHP 管理模板。如果你不介意这个问题 - 选择它们,那么你不需要这个包。否则,这个小型包可以帮助你以简单和可维护的方式设置 <script type="text/x-template"> 方案。

安装

  1. composer require arrilot/vue-templates-in-php

使用

首先创建一个这样的辅助程序

function vue()
{
    static $vue = null;

    if ($vue === null) {
        $vue = new \Arrilot\VueTemplates\TemplateManager('/absolute/path/to/directory/where/you/want/to/store/templates/');
    }

    return $vue;
}

或者如果你有一个服务容器,将 TemplateManager 对象放入其中。

添加

<?php vue()->printTemplates() ?>

在页脚的某个地方(在启动 vue 应用程序的脚本 之上)。

现在你可以开始制作模板了。例如,让我们假设你想要创建一个用于主菜单的组件。

  1. 在 js 中创建一个没有模板的组件。
  2. 在传递给 TemplateManager 的目录内创建一个 main-menu.php 文件。这是一个组件模板。你不需要向它添加任何 <script type="text/x-template"><template> 标签。这是在幕后完成的。
  3. 在需要它的页面上注册此模板 - vue()->addTemplate('main-menu')
  4. 现在你可以在步骤 1 中创建的 Vue 组件中引用它: template: '#vue-main-menu-template'