英文:
How to efficiently handle a Vue3/Quasar project with dynamic and static image paths
问题
我有一个问题,我认为这个问题并不独特。有时我的夸克星应用程序中,我使用vite/quasar快捷方式来引用资源,如下所示:
<q-carousel-slide :name="2" class="column no-wrap flex-center q-pa-none">
<q-img src="~assets/imgs/carousel/service-upgrade.jpg" style="height:100%">
<div class="column no-wrap flex-center" style="width: 100%;height:100%">
<q-img src="~assets/imgs/carousel/logo-white.png" style="max-width:250px" class="q-mb-lg"></q-img>
<h1 class="text-h1 text-weight-bolder text-white q-my-none">
ELECTRICAL UPGRADES
</h1>
<h4 class="text-h4 text-weight-light q-mt-xs q-mb-lg text-white">
NEW WIRING & SERVICE UPGRADES
</h4>
<q-btn outline color="white" class="q-my-5 my-btn" label="GET A QUOTE" size="lg" @click="layoutStore.toggleContactUsDrawer"></q-btn>
</div>
</q-img>
</q-carousel-slide>
然而,有时候将这些路径放入数组中以缩短我的代码并使用v-for会更容易,如下所示:
<q-tab-panel name="lighting">
<div class="row q-pa-md block-wrapper text-center q-col-gutter-md" style="margin-bottom:100px;">
<div v-for="item in imagesArray[0]['lighting']" :key="item.id" class="col-sm-4 col-xs-6">
<q-img loading="eager" :src="getImageUrl(item.link)" class="rounded-borders text-right" :ratio="16/9" @click="openFullScreenImage(getImageUrl(item.link))"></q-img>
</div>
</div>
</q-tab-panel>
上述代码中的相对路径:
const imagesArray = [{
panels: [
{ id: 1, link: '../assets/imgs/recentwork/panels/panel 1.jpg' },
{ id: 2, link: '../assets/imgs/recentwork/panels/panel 2.jpg' },
// 其他面板图片的相对路径
],
lighting: [
{ id: 1, link: '../assets/imgs/recentwork/lighting/lighting 13.jpg' },
{ id: 2, link: '../assets/imgs/recentwork/lighting/lighting 22.jpg' },
// 其他照明图片的相对路径
],
commercial: [
{ id: 1, link: '../assets/imgs/recentwork/commercial/commercial 1.jpg' },
{ id: 2, link: '../assets/imgs/recentwork/commercial/commercial 2.jpg' },
// 其他商业图片的相对路径
],
}]
这种方式会导致我必须维护两个图像文件夹,一个在src文件夹中,另一个在public/assets/imgs文件夹中。这样每次都要复制图像非常麻烦,而且如果需要分别维护这两个文件夹,就更加麻烦了。
如何只使用src资产图像文件夹而不使用public文件夹呢?这个问题也很烦人,有没有解决方法?
英文:
I have a problem that I would not think is unique. There are times my quasar application I use the vite/quasar shortcut for assets as below:
<q-carousel-slide :name="2" class="column no-wrap flex-center q-pa-none">
<q-img src="~assets/imgs/carousel/service-upgrade.jpg" style="height:100%">
<div class="column no-wrap flex-center" style="width: 100%;height:100%">
<q-img src="~assets/imgs/carousel/logo-white.png" style="max-width:250px" class="q-mb-lg"></q-img>
<h1 class="text-h1 text-weight-bolder text-white q-my-none">
ELECTRICAL UPGRADES
</h1>
<h4 class="text-h4 text-weight-light q-mt-xs q-mb-lg text-white">
NEW WIRING & SERVICE UPGRADES
</h4>
<q-btn outline color="white" class="q-my-5 my-btn" label="GET A QUOTE" size="lg" @click="layoutStore.toggleContactUsDrawer"/>
</div>
</q-img>
</q-carousel-slide>
However, there are also times where its just easier to put these paths in an array to shorten my code with a v-for such as below:
<q-tab-panel name="lighting">
<div class="row q-pa-md block-wrapper text-center q-col-gutter-md" style="margin-bottom:100px;">
<div v-for="item in imagesArray[0]['lighting']" :key="item.id" class="col-sm-4 col-xs-6">
<q-img loading="eager" :src="getImageUrl(item.link)" class="rounded-borders text-right" :ratio="16/9" @click="openFullScreenImage(getImageUrl(item.link))"/>
</div>
</div>
</q-tab-panel>
Script with relative paths for code above:
const imagesArray = [{
panels:[
{ id: 1, link: '../assets/imgs/recentwork/panels/panel 1.jpg'},
{ id: 2, link: '../assets/imgs/recentwork/panels/panel 2.jpg'},
{ id: 3, link: '../assets/imgs/recentwork/panels/panel 3.jpg'},
{ id: 4, link: '../assets/imgs/recentwork/panels/panel 4.jpg'},
{ id: 5, link: '../assets/imgs/recentwork/panels/panel 5.jpg'},
{ id: 6, link: '../assets/imgs/recentwork/panels/panel 6.jpg'},
{ id: 7, link: '../assets/imgs/recentwork/panels/panel 7.jpg'},],
lighting: [
{ id: 1, link: '../assets/imgs/recentwork/lighting/lighting 13.jpg'},
{ id: 2, link: '../assets/imgs/recentwork/lighting/lighting 22.jpg'},
{ id: 3, link: '../assets/imgs/recentwork/lighting/lighting 26.jpg'},
{ id: 4, link: '../assets/imgs/recentwork/lighting/lighting 24.jpg'},
{ id: 5, link: '../assets/imgs/recentwork/lighting/lighting 28.jpg'},
{ id: 6, link: '../assets/imgs/recentwork/lighting/lighting 17.jpg'},
{ id: 7, link: '../assets/imgs/recentwork/lighting/lighting 27.jpg'},
{ id: 8, link: '../assets/imgs/recentwork/lighting/lighting 30.jpg'},
{ id: 9, link: '../assets/imgs/recentwork/lighting/lighting 12.jpg'},
],
commercial: [
{ id: 1, link: '../assets/imgs/recentwork/commercial/commercial 1.jpg'},
{ id: 2, link: '../assets/imgs/recentwork/commercial/commercial 2.jpg'},
{ id: 3, link: '../assets/imgs/recentwork/commercial/commercial 3.jpg'},
{ id: 4, link: '../assets/imgs/recentwork/commercial/commercial 4.jpg'},
{ id: 5, link: '../assets/imgs/recentwork/commercial/commercial 5.jpg'},
{ id: 6, link: '../assets/imgs/recentwork/commercial/commercial 6.jpg'},
{ id: 7, link: '../assets/imgs/recentwork/commercial/commercial 7.jpg'},
{ id: 8, link: '../assets/imgs/recentwork/commercial/commercial 8.jpg'},
{ id: 9, link: '../assets/imgs/recentwork/commercial/commercial 9.jpg'},
],
}]
When I do it this way I find myself having to keep two folders of images. One in the src folder then I also have to maintain an identical folder in the public/assets/imgs folder. So this is a pain to keep recopying over the images but it is even more of a pain if i were to have to maintain both seperately as well.
How can you just use the src assets img folder for everything rather than using this public one which is quite annoying to maintain as well?
答案1
得分: 0
非公开资产需要导入。在使用 Vite 时,可以使用 new URL(url, import.meta.url) 来完成这个操作。您只需要将数组更改为以下方式:
panels:[
{ id: 1, link: new URL('.. /assets/imgs/recentwork/panels/panel 1.jpg', import.meta.url).href },
{ id: 2, link: new URL('.. /assets/imgs/recentwork/panels/panel 2.jpg', import.meta.url).href },
{ id: 3, link: new URL('.. /assets/imgs/recentwork/panels/panel 3.jpg', import.meta.url).href },
...
英文:
non-public assets need to be imported. Using vite, this can be done with new URL(url, import.meta.url). You would just change your array to be along the lines of:
panels:[
{ id: 1, link: new URL('../assets/imgs/recentwork/panels/panel 1.jpg', import.meta.url).href },
{ id: 2, link: new URL('../assets/imgs/recentwork/panels/panel 2.jpg', import.meta.url).href },
{ id: 3, link: new URL('../assets/imgs/recentwork/panels/panel 3.jpg', import.meta.url).href },
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论