Laravel&#x2B中的默认持久布局;惯性Vite
在Laravel应用程序中设置惯性的一种方式中,我可以从“ createInertiaapp函数”中调整Resolve属性:从:
{
...,
resolve: name => import("./Pages/${name}"),
...
}
允许
{
...,
resolve: name => {
const page = require("./Pages/${name}").default
if(!page.layout) {
page.layout = DefaultLayoutFile
}
},
...
}
我手动通过页面中使用的默认布局文件。
但是随着VITE成为默认资产捆绑器,并根据 a>,我必须使用ResolvePageComponent
功能import.meta.glob
作为第二个参数,以指示VITE要捆绑的文件。
问题是从此resolvePageComponent
返回导入,因此我无法像我通常从requient函数中访问默认对象一样访问默认对象。
因此,我无法将默认布局文件附加到导入页面上。
有人能为此找到解决方法吗?
In the previous way of setting up inertia in a laravel app, I could tweak the resolve property in the `createInertiaApp function from:
{
...,
resolve: name => import("./Pages/${name}"),
...
}
To
{
...,
resolve: name => {
const page = require("./Pages/${name}").default
if(!page.layout) {
page.layout = DefaultLayoutFile
}
},
...
}
To allow me manually pass a default layout file to be used in pages.
But with Vite becoming the default asset bundler and according to the docs, I must use a resolvePageComponent
function which takes in import.meta.glob
as a second argument to instruct Vite which files to bundle.
Problem here is the import gets returned from this resolvePageComponent
so I cannot access the default object like I normally will from a require function.
So I have not been able to attach a default layout file to imported pages.
Has anyone been able to find a workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您类似地导入默认布局文件(请记住,请不要再在导入中使用 @,因为仅出于静态分析原因相对路径起作用):
从'./ layouts/defaultlayoutfile.vue.vue'
'使用以下代码使默认布局与惯性和vite一起使用:
[UPDATE 2022-08-01]
由于这仍在收到视图,但我展示如何获得
@
在Vite中工作将是有用的。需要
PATH
下面的导入vite.config.js
,然后添加
resolve
添加到您的配置中:现在
@
将指向您的Laravel root,您可以从任何地方动态导入组件:例如
从'@/layouts/layouttop.vue'导入layouttop
现在将指向
/resources/js/layouts/layouttop.vue
请记住,导入VUE SFC文件时,Vite需要
.vue
扩展。Assuming you imported your default layout file like this (remember to not use @ in imports anymore as only relative paths work for static analysis reasons):
import DefaultLayoutFile from './Layouts/DefaultLayoutFile.vue'
You can use the following code to get default layout working with Inertia and Vite:
[UPDATE 2022-08-01]
Since this is still receiving views, I though it would be useful to show how to get the
@
working in imports in Vite.Require
path
below your imports invite.config.js
And then add
resolve
into your config below:Now
@
will point to your Laravel root and you can import components dynamically from anywhere:For example
import LayoutTop from '@/Layouts/LayoutTop.vue'
will now point to
/resources/js/Layouts/LayoutTop.vue
Remember that Vite needs the
.vue
extension when importing Vue SFC files.等待可接受的答案的版本
The async await version of the accepted answer
对于
laravel
和vue3.js
和intertia
和vite> vite
setup(laravel9 )代码>及以上)
转到
资源\ JS \ app.js
更改为
laravel
和vue3.js
和inertia
和webpack
setup(laravel8
及以下)转到
Resources \ JS \ app.js
更改为
For Default Persistent Layout in
Laravel
andVue3.js
andInertia
andVite
Setup(Laravel9
and above)Go to
resources\js\app.js
Change to
For Default Persistent Layout in
Laravel
andVue3.js
andInertia
andWebpack
Setup(Laravel8
and below)Go to
resources\js\app.js
Change to
@ResidualFlash添加条件也有效:
@residualflash Adding condition works as well:
使用 vite with interertia prestia
This worked from me using the vite with inertia preset