Vue项目中结合Vue-layer实现弹框式编辑功能

1. 实现效果

在这里插入图片描述

2.实现原理

在父组件中点击编辑按钮,将当前点击对象的id传给子组件,子组件根据id修改相应的内容

父组件中代码:
//放置编辑按钮的位置
<button type="button" class="layui-btn  layui-btn-normal  layui-btn-sm" v-on:click="edit(manage.id)"><i class="layui-icon">&#xe642;</i>编辑</button>
// 在methods中设计edit()方法
//需要先引入编辑组件
import EditManage from './EditManage'
 edit(id){
        this.$layer.iframe({
          type:2,
          title:"编辑",
          area:['600px','450px'],
          shade:true,
          offset:'auto',
          content:{
            content:EditManage,//传递的编辑组件主线
            parent:this,
            data:{
             info:{id:id}// 传递的要编辑内容的id值
            }
          }
        })
      },
子组件EditManage代码
<template>
  <div class="editmanage container">
    <form  class="form" v-on:submit="editManage">
      <div class="form-group">
        <label>账号</label>
        <input type="text" required placeholder="账号" autocomplete="off" class="form-control" v-model="manage.account">
      </div>
      <div class="form-group">
        <label>用户名</label>
        <input type="text" required placeholder="用户名" autocomplete="off" class="form-control" v-model="manage.username">
      </div>
      <div class="form-group">
        <label >密码</label>
        <input type="password" required placeholder="密码" autocomplete="off" class="form-control" v-model="manage.password">
      </div>

      <div class="form-group">
        <label >权限</label>
        <select name="authority"  class="form-control" v-model="manage.authority">
          <option value="超级管理员"  >超级管理员</option>
          <option value="普通管理员" >普通管理员</option>
        </select>
      </div>
      <button type="submit" class="btn btn-info">立即提交</button>
    </form>
  </div>
</template>

<script>
  export default {
    name: 'addmanage',
    data () {
      return {
        manage:{},
        form:{}
      }
    },
    props:{
    //接收父组件传来的id值
      info:{
        type:Object,
        default:()=>{
          return {}
        }
      },
      layerid:{
        type:String,
        default:""
      },
      lydata:{
        type:Object,
        default:()=>{
          return {}
        }
      }
    },
    methods:{
    //用来显示对应id的编辑内容,在created中调用
      fetchManage(id){
        this.$http.get('http://localhost:3000/manage/'+id)
          .then(function (response) {
            this.manage=response.body;
          })
      },
      editManage(e){
        if(!this.manage.account||!this.manage.username||!this.manage.password||!this.manage.authority){
          this.$layer.msg("请添加对应信息!")
        }else{
          let updateManage={
            account:this.manage.account,
            username:this.manage.username,
            password:this.manage.password,
            authority:this.manage.authority
          };      this.$http.put("http://localhost:3000/manage/"+this.info.id,updateManage)
            .then(function (response) {
            //关闭父组件中的编辑弹框
              this.$layer.close(this.layerid);
              //弹出提示信息,默认时间为3秒
              this.$layer.msg("修改管理员信息成功!");


            });
          e.preventDefault()
        }
        e.preventDefault()
      }
    },
    created(){
    // this.info.id父组件传给子组件的id值
      this.fetchManage(this.info.id);

    }
  }
</script>


<style scoped>
  .editmanage{
    margin:8px 20px 20px 20px;
  }
</style>

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以先安装 `vue-quill-editor`: ```bash npm install vue-quill-editor --save ``` 然后在需要使用富文本编辑器的组件引入: ```javascript import VueQuillEditor from 'vue-quill-editor' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' export default { components: { VueQuillEditor }, data() { return { content: '' } } } ``` 这里引入了三个样文件,分别是 `quill.core.css`、`quill.snow.css` 和 `quill.bubble.css`,它们分别对应了三种不同的编辑器主题,可以根据需求选择使用。 接着在模板使用组件: ```html <template> <div> <vue-quill-editor v-model="content"></vue-quill-editor> </div> </template> ``` 这样就完成了一个简单的富文本编辑器的使用。注意,这里使用了 `v-model` 将编辑器的内容与 `content` 变量进行了双向绑定,所以在提交表单时可以直接使用 `content` 变量获取编辑的内容。 当然,还可以根据自己的需求设置更多的参数,例如: ```html <vue-quill-editor v-model="content" :options="{ placeholder: '请输入内容', modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['link', 'image', 'video'] ] } }" ></vue-quill-editor> ``` 这里设置了编辑器的 `placeholder` 和 `modules.toolbar` 参数,让编辑器拥有更多的功能。具体的参数设置可以参考 `vue-quill-editor` 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值