区块(block)

    以上注册了 formView block

    使用 block

    每个block本质都是,可以通过区块名 name 来引用:<ams-block name="formView" />,完整示例如下:

    1. <template>
    2. <ams-block name="formView" />
    3. </template>
    4. <script>
    5. import ams from '@ams-team/ams';
    6. // 注册资源
    7. ams.resource('demo', {
    8. key: 'id',
    9. api: { // 数据接口相关
    10. prefix: 'https://easy-mock.com/mock/5a0023effbbb09615044cb82/',
    11. list: 'list'
    12. },
    13. fields: { // 字段
    14. id: {
    15. type: 'text',
    16. label: 'id'
    17. },
    18. testArrays: {
    19. type: 'array',
    20. label: 'testTexts',
    21. field: {
    22. type: 'text',
    23. label: 'testTexts',
    24. props: {
    25. 'suffix-icon': 'el-icon-service'
    26. }
    27. }
    28. }
    29. }
    30. })
    31. // 注册区块
    32. ams.block('formView', {
    33. type: 'form',
    34. resource: 'demo',
    35. ctx: 'view',
    36. data: {},
    37. events: {
    38. init: '@read @console'
    39. },
    40. actions: {
    41. console() {
    42. console.log('@console action', this.block.data.id);
    43. }
    44. }
    45. });
    46. </script>

    子 blocks

    可以通过子 blocks 配置实现 block 的多级嵌套,组合成复杂的页面

    1. ams.block('dialog', {
    2. type: 'dialog', // 弹窗类型区块
    3. blocks: {
    4. form: {
    5. type: 'form', // 嵌套一个表单区块
    6. resource: 'resource',
    7. blocks: {
    8. div: {
    9. type: 'component', // 表单区块再嵌套一个万能区块
    10. options: {
    11. is: 'div'
    12. }
    13. }
    14. }
    15. }
    16. });

    子 blocks 可以通过配置 slot 指定插入位置,不指定则插入到其内部主内容之后。

    如 router 的 block 支持配置 slot 为 menuTopmenuBottomnav 来使 block 插入到菜单头部,菜单底部,导航的位置(具体和对应 block 开放的插槽位置有关)

    block 支持返回 Promise 的函数,会在对应 block 就绪(resolve)后渲染指定 block,支持嵌套

    1. ams.block('formViewAll', function() {
    2. return new Promise((resolve, reject) => {
    3. setTimeout(() => {
    4. console.log('resolve formViewAll')
    5. resolve({
    6. type: 'form',
    7. resource: 'resource',
    8. ctx: 'view',
    9. data: {
    10. },
    11. style: {
    12. width: '50%'
    13. },
    14. events: {
    15. init: '@read @console'
    16. },
    17. actions: {
    18. console() {
    19. console.log('@console action', this.data.id)
    20. }
    21. }
    22. });
    23. }, 1000)
    24. })
    25. });

    actions

    • 类型:{ function({ name: string, value: any }): void }
    • 默认值:undefined配置示例:
    1. actions: {
    2. fieldChange({ name, value }) {
    3. if (name === 'testSwitch') {
    4. this.block.fields.testPassword.props.disabled = !value
    5. }
    6. },
    7. cancel() {
    8. this.$router.back()
    9. },
    10. getAlert() {
    11. // alert,自定义数据通过options配置
    12. this.emitEvent('alert', {
    13. message: '这是一个alert的弹框',
    14. options: {
    15. title: '我是一个alert的标题'
    16. }
    17. });
    18. },
    19. getPrompt() {
    20. // promt直接使用方式,参考https://element.eleme.cn/#/zh-CN/component/message-box
    21. this.$prompt('请输入邮箱', '提示', {
    22. confirmButtonText: '确定',
    23. cancelButtonText: '取消',
    24. inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
    25. inputErrorMessage: '邮箱格式不正确'
    26. }).then(({ value }) => {
    27. this.$message({
    28. type: 'success',
    29. message: '你的邮箱是: ' + value
    30. });
    31. }).catch(() => {
    32. this.$message({
    33. type: 'info',
    34. message: '取消输入'
    35. });
    36. });
    37. }
    38. }

    内置的action有如下

    field的数据变更触发可以通过在actions中定义fieldChange获取对应的值,方法参数为

    fieldChange({ field, name, value, path })

    events

    • 类型:object且{ [field: string]: string }
    • 默认值:undefinedevents是用来配置 block 的事件流,采用链式 action 调用,前一个 action 执行完之后再调用下一个 action,多个 action 之间用空格隔开

    事件流配置示例:

    init是AMS给每个区块内置的 event,在区块每次初始化时都会执行。

    如果希望多个异步action顺序依此执行,需要将action函数返回一个Promise或者使用async函数

    submit: '@validate @confirm:确认提交吗? @update'表示为:

    执行validate -> 执行confirm,传参确认提交吗 -> 执行

      • 通过@action:args传参,如@confirm:确认提交吗?,调用confirm事件,传参确认提交吗

    通过operations的配置可以配置各种操作、响应用户事件,用户对operation进行操作后会产生事件,通过对事件的处理就可以完成对用户操作的反馈

    operations结构类型如下:

    1. {
    2. [actionField: String]: {
    3. type: 'button' | 'icon' | 'reset' | 'field',
    4. event?: String,
    5. slot?: String,
    6. field?: String | FieldConfig, // field 特有
    7. props: {
    8. [field: String]: any
    9. },
    10. show?: String | Object | Function,
    11. style: {
    12. [styleName: String]: String
    13. }
    14. }
    15. }

    配置示例:

    1. operations: {
    2. editItem: {
    3. type: 'button',
    4. props: {
    5. type: 'primary',
    6. icon: 'el-icon-edit',
    7. circle: true
    8. }
    9. },
    10. removeItem: {
    11. type: 'button',
    12. props: {
    13. type: 'danger',
    14. icon: 'el-icon-delete',
    15. circle: true
    16. },
    17. show: 'testSwitch' // String类型:代表通过获取名为“testSwitch”的field值(this.data['testSwitch'])
    18. },
    19. deleteItem: {
    20. type: 'button',
    21. props: {
    22. type: 'danger',
    23. icon: 'el-icon-delete',
    24. circle: true
    25. },
    26. show: { // Object类型:代表通过获取名为“testSwitch”的field值和value指定的值进行比较,相等(==)就返回true,否则返回flase
    27. name: 'testSwitch',
    28. value: '1'
    29. }
    30. },
    31. statusItem: {
    32. type: 'button',
    33. props: {
    34. type: 'danger',
    35. icon: 'el-icon-delete',
    36. circle: true
    37. },
    38. show(data) { // Function类型:适合取决于多个条件比较后的结果
    39. console.log(data, this)
    40. return data.id === 1 && data.testSwitch;
    41. }
    42. }
    43. }

    参数说明

    • actionField:对应blocks中的events名字,详细用法参考:
    • event:指定调用event名,优先级大于 actionField
    • slot:指定operation所在插槽,如list支持 searchsmultipleSelect 定制搜索操作和多选操作
    • field:指定使用的field配置,使用String刚会使用本block内同名的field配置
    • props:透传至operation根节点的props配置
    • style:透传至operation根节点的style配置
    • show:满足show条件的才会显示,在list内是该行的值,在form内是data的值,如show:{name:'test',value:2} 当这一行的name字段的值为2时该operation会显示点击前往更深入的了解operations

    props 属性、on 事件、style 样式的定制

    block支持透传props、on和style 至 block 内部根元素(原生节点或者element-ui节点)

    等价于

    1. <img @click="()=>{console.log('click')}" style="{
    2. width: '100px',
    3. height: '100px',
    4. backgroundColor: '#007'

    通用配置

    参数类型是否必传说明
    typestringblock的类型
    resourcestring、object资源key或者是具体的资源
    dataobjectblock数据默认值
    ctxstring'edit' 为编辑态、'view' 为显示态
    optionsobjectblock选项
    configobject全局配置、同ams.config(config)
    fieldsobject可以合并覆盖当前block的资源内的field配置,点击前往
    styleobject样式配置
    propsobject补充属性,用于添加DOM属性或者透传至element组件,如disabled
    eventsobject配置events
    actionsobject配置actions
    operationsobject、array配置operations
    blocksobject、array子blocks配置,如为object则为具体的配置内容,如为array则是子blocks的key列表
    renderstring、boolean如果为true会自动渲染到body内,或者指定已有节点的querySelector