Skip to content

Commit 38c1d2c

Browse files
committed
更新自定义组件文档
1 parent 0c237cb commit 38c1d2c

File tree

1 file changed

+92
-56
lines changed

1 file changed

+92
-56
lines changed

docs/custom.md

Lines changed: 92 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 自定义扩展
1+
# 自定义组件
22

33
可以扩展属于自己业务逻辑的组件
44

@@ -87,6 +87,56 @@ npm run production
8787

8888
当然你还可以创建你的 路由 控制器 Model 等等,开发出一个具有业务逻辑的扩展
8989

90+
## 自定义组件的理解
91+
92+
由于前后端分离的架构,任何一个组件的实现都是先加载组件定义属性,根据属性渲染组件
93+
94+
可以说一切组件皆对象,所有要实现一个组件就必须有组件定义类(后端),Vue文件(前端)
95+
96+
后端定义好一个组件的 属性,前端根据属性去实现相对应的功能
97+
98+
自定义组件完全没限制,比如你可以使用内置已有的包功能,也可以安装新的包
99+
100+
101+
102+
## 内置已安装的npm包
103+
104+
```json
105+
"devDependencies": {
106+
"@chenfengyuan/vue-qrcode": "^1.0.2",
107+
"axios": "^0.19.2",
108+
"babel-plugin-component": "^1.1.1",
109+
"babel-plugin-import": "^1.13.0",
110+
"cross-env": "^5.1",
111+
"element-ui": "^2.13.0",//UI
112+
"laravel-mix": "^4.1.4",
113+
"lodash": "^4.17.15",
114+
"popper.js": "^1.12",
115+
"resolve-url-loader": "^2.3.1",
116+
"sass": "^1.20.1",
117+
"sass-loader": "7.*",
118+
"vue": "^2.6.11",
119+
"vue-bus": "^1.2.1",
120+
"vue-clipboard2": "^0.3.1",
121+
"vue-dplayer": "0.0.10",
122+
"vue-happy-scroll": "^2.1.1",
123+
"vue-router": "^3.1.6",
124+
"vue-template-compiler": "^2.6.10",
125+
"vue-waterfall2": "^1.9.6",
126+
"wangeditor": "^3.1.1",
127+
"@antv/g2plot": "^1.0.2",
128+
"awe-dnd": "^0.3.4",
129+
"babel-plugin-dynamic-import-webpack": "^1.1.0",
130+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
131+
"nprogress": "^0.2.0",
132+
"url-loader": "^3.0.0",
133+
"vue-nprogress": "^0.1.5",
134+
"vuex": "^3.1.3"
135+
}
136+
```
137+
138+
139+
90140
## 创建步骤
91141

92142
创建一个自定义组件需要三部
@@ -101,74 +151,46 @@ npm run production
101151

102152
### Form字段组件
103153

104-
`src/components`下创建一个PHP类
154+
`src/components`下创建一个组件定义类,格式如下,必须继承`SmallRuralDog\Admin\Components\Component`
105155

106156
```php
107-
namespace Smallruraldog\MissMeijiuAdmin\Components;
108157

109158
use SmallRuralDog\Admin\Components\Component;
110159
//需要继承 Component
111-
class GoodsSku extends Component
160+
class MyInput extends Component
112161
{
113162
//组件的名称,等下注册vue组件的时候名称需要一致
114-
protected $componentName = "GoodsSku";
115-
116-
/** 示例属性 *******************************/
117-
118-
//当前所有规格
119-
protected $goodsAttrs = [];
120-
//添加规格接口
121-
protected $addGoodsAttrUrl;
122-
protected $addGoodsAttrValueUrl;
123-
protected $uploadComponent;
124-
protected $imageComponent;
125-
/*********************************/
126-
127-
//需要隐藏的属性,设置后不会在json中输出
163+
protected $componentName = "MyInput";
164+
165+
//需要隐藏的属性,设置后不会在json中输出,这个功能由父类实现
128166
public $hideAttrs = [];
129167

130-
public function __construct($value = [])
131-
{
132-
//字段初始值
133-
$this->componentValue($this->getValue([]));
134-
135-
/** 示例代码 *******************************/
136-
$goodsAttrModel = new GoodsAttr();
137-
$this->goodsAttrs = $goodsAttrModel->allAttrs();
138-
$this->addGoodsAttrUrl = route("addGoodsAttr");
139-
$this->addGoodsAttrValueUrl = route("addGoodsAttrValue");
140-
$this->uploadComponent = Upload::make()->width(130)->height(130);
141-
$this->imageComponent = Image::make()->size(30, 30)->className("mr-10");
142-
/*********************************/
143-
}
144-
//定义一个make
145-
public static function make($value = [])
168+
//定义一个make,这里的$value 是你组件的默认值,类型由你来决定
169+
public static function make($value=null)
146170
{
147171
return new GoodsSku($value);
148172
}
149173

150-
//自定义字段输出值,在表单编辑时使用
174+
//自定义字段输出值,在表单编辑时会调用这个方法获取值,如果没定义则使用默认值
151175
public function getValue($data)
152176
{
153-
return [
154-
'goods_attrs' => [],
155-
'goods_sku_list' => []
156-
];
177+
return $data;
157178
}
158179
}
159180
```
160181

161-
`resources\js\components`下创建vue文件,例如`GoodsSku.vue`
182+
`resources\js\components`下创建vue文件,例如`MyInput.vue`
183+
184+
162185

163186
```vue
164187
<template>
165-
<!--这是属性示例用法-->
188+
<!--这是属性示例用法,具体要实现什么功能,自由发挥-->
166189
<el-input
167190
:style="attrs.style"
168191
:class="attrs.className"
169-
:validate-event="attrs.validateEvent"
170-
:value="value" <!--绑定字段的值-->
171-
@input="onChange" <!--改变字段的值-->
192+
:value="value"
193+
@input="onChange"
172194
>
173195
</el-input>
174196
</template>
@@ -186,10 +208,6 @@ class GoodsSku extends Component
186208
default: null
187209
}
188210
},
189-
data() {
190-
return {
191-
};
192-
},
193211
//上层实现了v-model功能
194212
model: {
195213
prop: "value",
@@ -210,14 +228,14 @@ class GoodsSku extends Component
210228
```js
211229
VueAdmin.booting((Vue, router, store) => {
212230
//....................
213-
Vue.component("GoodsSku", require('./components/GoodsSku').default),
231+
Vue.component("MyInput", require('./components/MyInput').default),
214232
});
215233
```
216234

217235
在代码中使用组件
218236

219237
```php
220-
$form->item("goods_sku", "产品规格")->displayComponent(GoodsSku::make())
238+
$form->item("goods_sku", "产品规格")->component(MyInput::make())
221239
```
222240

223241
### Grid字段组件
@@ -289,11 +307,25 @@ const GridColumnComponent = {
289307
}
290308
```
291309

310+
`extend.js`注册组件
311+
312+
```js
313+
Vue.component('Boole', require('xxx').default);
314+
```
292315

316+
打包你的组件
317+
318+
```shell
319+
npm run production
320+
```
321+
322+
使用组件
323+
324+
```php
325+
$grid->column('status', "状态")->width(100)->align("center")->component(Boole::make());
326+
```
293327

294-
### Grid字段编辑组件
295328

296-
正在编写中....
297329

298330
### Grid操作组件
299331

@@ -511,7 +543,7 @@ class ToolButton extends BaseAction
511543

512544
创建vue文件
513545

514-
```php
546+
```vue
515547
<template>
516548
<el-button
517549
:type="attrs.type"
@@ -595,9 +627,11 @@ $fansListGrid->toolbars(function (Grid\Toolbars $toolbars) use ($app_id) {
595627

596628

597629

598-
### Page组件
630+
### 基础组件
599631

600-
如果内置的组件无法满足你的需求,那么就需要自定义了
632+
基础组件的Vue的props中,只能获取当前组件的属性,没有其他附加的props
633+
634+
> 其实工具栏组件也是基础组件
601635
602636
创建组件定义类
603637

@@ -645,6 +679,8 @@ public function index(Content $content)
645679

646680
由于各个组件需要通信,所以需要监听或触发各种事件
647681

682+
当然,在你自定义的组件中也可以开放事件给其他组件调用
683+
648684
```js
649685
//监听事件
650686
this.$bus.on("EventName",()=>{
@@ -694,7 +730,7 @@ this.$bus.emit("tableSetLoading",false);
694730
this.$bus.emit("pageReload");
695731
```
696732

697-
关闭Dialog
733+
### 关闭Dialog
698734

699735
```php
700736
this.$bus.emit("closeDialog");

0 commit comments

Comments
 (0)