laravel blade模板引擎组件使用

laravel blade模板引擎组件使用

在这里插入图片描述
如上图所示,表格搜索查询时,当有多个筛选框时觉得写太多 <input /> 有点麻烦,于是看看可不可以使用 vue 组件,搜索一番发现文档中有关于 blade 中使用组件,如下:

  • 命令框输入以下代码,会在 resources/views 下生成 components文件夹,以及在 app/View/下生成 Components 文件夹

        php artisan make:component searchInputNumber
    
  • 打开 app/View/Components/searchInputNumber.php文件,写入以下代码

    	<?php
    		namespace App\View\Components;
    		use Illuminate\View\Component;
    		
    		class searchInputNumber extends Component
    		{
    		    public $title;
    		
    		    public $attr;
    		
    		    /**
    		     * Create a new component instance.
    		     *
    		     * @return void
    		     */
    		    public function __construct($title,$attr)
    		    {
    		        $this->title = $title;
    		        $this->attr = $attr;
    		    }
    		
    		    /**
    		     * Get the view / contents that represent the component.
    		     *
    		     * @return \Illuminate\View\View|string
    		     */
    		    public function render()
    		    {
    		        return view('components.search-input-number');
    		    }
    		}
    
    
  • 新建 search_input_number.blade.php 文件(命名表示这是一个input 类型为 number的输入框),组件代码如下所示:

    	<div class="layui-inline">
    		<label class="layui-form-label">{{ $title }}</label>
    		<div class="layui-input-inline">
        		<input type="number" id="search_user_name" name="{{ $attr }}"  placeholder="请输入{{ $title }}"  class="layui-input">
    		</div>
    	</div>
    

    ps: {{ $title }}{{ $attr }} 表示动态变量,可以根据需要添加多个

    在其他模板调用组件代码如下:

    	<x-search_between_number title="收入金额" attr="earnings_num"></x-search_between_number>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值