首页 > 解决方案 > 无法从本地路径加载Nativescript vue中的图片,但是用http/https就可以了,为什么?

问题描述

我目前正在开发一个小的 Nativescript vue 项目,我对这项技术很陌生。我正在尝试从本地路径加载图像。当 src 是和 http 链接时,一切都很好,但是资产文件夹中的本地图像没有任何作用。我正在运行 Nativescript 6.0.2 和 Vue 3.9.3。我按照 Nativescript vue 网站上的指南启动项目。

我尝试了不同的东西。

<Image src="../assets/images/NativeScript-Vue.png" stretch="none" col="2" row="0"/>不工作

<Image src="~/assets/images/NativeScript-Vue.png" stretch="none" col="2" row="0"/>不工作

<Image src="https://www.google.com/images/errors/logo_sm_2.png" stretch="none" col="2" row="0"/>确实有效

<Image :src="native_img" stretch="none" col="2" row="0"/>

data() {
            return {
                native_img: "~/assets/images/NativeScript_logo.png"
            };

不工作

<Image :src="native_img" stretch="none" col="2" row="0"/>
data() {
            return {
                native_img: "https://play.nativescript.org/dist/assets/img/NativeScript_logo.png"
            };
        }

确实有效

而我的项目文件夹结构是 V2 项目结构 https://nativescript-vue.org/en/docs/getting-started/upgrade-guide/

感谢您的时间 ;)

标签: imagevue.jsnativescriptnativescript-vue

解决方案


如果您的文件的位置是这样的:

- app
   - App_Resources
   - assets
     - images

然后你应该像这样访问你的图像:

<Image src="~/assets/images/imageName.png" stretch="none"></Image>

推荐阅读