由于 Laravel-admin 采用的是 pjax 的方式刷新頁面,意味著很多頁面刷新的操作,并不是刷新整個 document,而是從服務器拿到部分 document,再通過類似 $(“#pjax-container”).html(newPart) 的方式更新的。
這就造成一個問題,每次 pjax 刷新,都會破壞 vue 的 dom 映射。
所以理論上有2種方法解決:
重新綁定一下 vue 的映射關系
在某些頁面禁止 pjax
1 太難搞,而且沒啥資料,放棄。2 的話比較可行。
部分禁止 pjax
打開 public/vendor/laravel-admin/laravel-admin/laravel-admin.js
添加代碼:
// 不使用 pjax 刷新的頁面$(document).on('pjax:beforeReplace', function (e, options) { // console.log(arguments) var freshPaths = [ ///admin.*//products/, ] for (let path of freshPaths) { if (path.test) { if (path.test(e.state.url)) { location.reload() return false } } else if (options.url.search(path) !== -1) { location.reload() return false } }})使用自定義 view
很多時候我們并不需要大動干戈地建立一個全部的 view,只需要在內置 view 中稍作修改。
這時候,我們需要先自定義一個 Content 類:
use Encore/Admin/Layout/Content;class MyContent extends Content { public function render() { $items = [ 'header' => $this->header, 'description' => $this->description, 'breadcrumb' => $this->breadcrumb, 'content' => $this->build(), ]; return view('admin.content', $items)->render(); }}然后引用它:
public function index(MyContent $content) { return $content ->header('product') ->description($this->brand) ->body($this->grid()); } 這樣一來,每次進入到 index 頁面,都會渲染 admin.content 這個 view 。
view 的內容直接 copy 自 vendor/encore/laravel-admin/resources/views/content.blade.php
在 view 里插入 vue 組件
添加2部分代碼即可。
第一部分是初始化 vue app:
<script data-exec-on-popstate> // boot up the demo $(function () { // vapp window.vapp = new Vue({ el: '#app', data () { return { status: { showGalleryEditor: false, }, store: { images: [], el: '', }, } }, components: {}, methods: { startGalleryEditing (event) { this.status.showGalleryEditor = true this.store.pk = $(event.target).parent().find('ul').data('pk') this.store.images = $(event.target).parent().find('img').toArray().map((e) => e.getAttribute('src')) window.p = $(event.target).parent().find('ul') }, }, }) }) </script>第2部分是插入組件:
新聞熱點
疑難解答
圖片精選