laravel-admin中使用自定义ueditor编辑器刷新的问题

我已经在laravel-admin中定义好了组件,可以用,但是每次使用都要刷新才行
当我点击编辑进去的时候是这样,只有当我刷新的时候才会变成第二张图的样子

clipboard.png

clipboard.png

我想知道怎样才能不刷新就直接变成第二章图的样子。。。。

组件是这样写的,参照的官方文档

<?php
namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class UEditor extends Field
{
    protected $view = 'admin::form.editor';
    protected static $css = [

    ];

    protected static $js = [
       'vendor/ueditor/ueditor.config.js',
        'vendor/ueditor/ueditor.all.js',
    ];

    public function render()
    {
        $cs=csrf_token();
        $config=config('ueditor.route.name');
        $this->script = <<<EOT
          window.UEDITOR_CONFIG.serverUrl = '$config'
 var ue = UE.getEditor('{$this->id}');
    ue.ready(function() {
        ue.execCommand('serverparam', '_token', '$cs'); // 设置 CSRF token.
    });
  
EOT;
        return parent::render();

    }
}
阅读 6.2k
5 个回答

组件代码如下写,可以解决这个问题:
(采用了楼上的解决方案,声望不够,不能给他:yangzuwei 点赞)

<?php
namespace App\Admin\Extensions;
use Encore\Admin\Form\Field;
/**
 * 百度编辑器
 * Class uEditor
 * @package App\Admin\Extensions
 */
class uEditor extends Field
{
    // 定义视图
    protected $view = 'admin.uEditor';

    // css资源
    protected static $css = [];

    // js资源
    protected static $js = [
        '/laravel-u-editor/ueditor.config.js',
        '/laravel-u-editor/ueditor.all.min.js',
        '/laravel-u-editor/lang/zh-cn/zh-cn.js'
    ];

    public function render()
    {
        $this->script = <<<EOT
        UE.delEditor('ueditor');
        var ue = UE.getEditor('ueditor'); // 默认id是ueditor
        ue.ready(function () {
            ue.execCommand('serverparam', '_token', '{{ csrf_token() }}');
        });
EOT;
        return parent::render();
    }
}
新手上路,请多包涵

应该是用了vue吧,要把ueditor写成组件

<template>
  <div ref="editor"></div>
</template>

<script>

  import './ueditor.config';
  import './ueditor.all';
  import './lang/zh-cn/zh-cn';


  export default {
    data() {
      return {
        id: parseInt(Math.random()*1000)+'ueditorId',
      };
    },
    props: {
      value: {
        type: String,
        default: null,
      }
    },
    watch: {
      // value: function value(val, oldVal) {
      //   this.editor = UE.getEditor(this.id);
      //   if (val !== null) {
      //     this.editor.setContent(val);
      //   }
      // },
    },
    mounted() {

      var _this = this;
      this.$nextTick(function f1() {
        // 保证 this.$el 已经插入文档

        this.$refs.editor.id = this.id;
        this.editor = UE.getEditor(this.id, this.config);

        this.editor.ready(function f2() {
          this.editor.setContent(this.value==null?'<p></p>':this.value);
          this.editor.addListener("contentChange afterExecCommand", function () {
            const wordCount = _this.editor.getContentLength(true);
            const content = _this.editor.getContent();
            const plainTxt = _this.editor.getPlainTxt();
            _this.$emit('input', { wordCount: wordCount, content: content, plainTxt: plainTxt });
          }.bind(this));

          // this.$emit('ready', this.editor);
        }.bind(this));
      });
    },
  };
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进