简体   繁体   English

使用 Vue.js 显示图像文件

[英]Show images files using Vue.js

I am new in Vue, i am doing examples using Vue 2.0, i am have this structure我是 Vue 的新手,我正在使用 Vue 2.0 做例子,我有这个结构

-src
 - assets
    --> logo.png
 - components
    --> beging.vue

i am doing changes in beging.vue and i have this line我正在 beging.vue 中进行更改,并且我有这条线

<template>
  <div class="container">
     <h1>Página de Inicio</h1>
     <hr>
     <img :src="require('../assets/logo.png')">
   </div>
</template>

The problem is that in the moment i making the compilation and i publish this in my local server the image always is hidden but before to compilate i can see the image fine.问题是,在我进行编译并将其发布到本地服务器的那一刻,图像始终是隐藏的,但在编译之前,我可以很好地看到图像。

Is important mentionate that in my local server i have this structure重要的是,在我的本地服务器中,我有这个结构

- assets
- dist
 .htaccess
- index.html

in theory all information exist inside to dist and yes is correct in dist exist:理论上,所有信息都存在于 dist 中,并且在 dist 中是正确的:

  • build.js构建.js
  • build.js.map构建.js.map
  • logo.png标志.png

But the imagen never appear.但图像从未出现。 Could you help me to understand what is the problem?你能帮我理解是什么问题吗?

Thank u.感谢你。

//UPDATE //更新

Exist a important point, as i am using apache is neccesary make a .htaccess file in my case i am working using subfolders, according the vue documentation if the file will be in subfolder thge configuration must be:存在一个重要的点,因为我使用 apache 是必要的,在我的情况下制作一个 .htaccess 文件我正在使用子文件夹,根据 vue 文档,如果文件将在子文件夹中,那么配置必须是:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

In my case the subfolder that i have is "test" when i review the code usign the inspector code from browser i can see this:在我的情况下,当我使用浏览器中的检查器代码查看代码时,我拥有的子文件夹是“test”,我可以看到:

<img src="./src/assets/logo.png?82b9c7a5a3f405032b1db71a25f67021">

But if i change this manually and put this:但是,如果我手动更改并输入:

  <img src=".test/src/assets/logo.png?82b9c7a5a3f405032b1db71a25f67021">

the image appear, but which is the configuration for put in htaccess file?图像出现,但放入htaccess文件的配置是什么?

the best way is to add File Loader.最好的方法是添加文件加载器。

         <template>
        <img :src="image"
        </template>
        <script>

        import image from 'relative/path/to/image'
        export default {
        data() {
            return {
            image
            };
        }
        };

Use @ sign to point to root folder使用@符号指向根文件夹

<template>
  <div class="container">
     <h1>Página de Inicio</h1>
     <hr>
     <img :src="require('@/assets/logo.png')">
   </div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM