简体   繁体   English

来自prop的Vue.js组件img src?

[英]Vue.js component img src from prop?

I'm trying to do the following, but the image doesn't output correctly.我正在尝试执行以下操作,但图像输出不正确。 Where I display the image in display.vue, if I print {{ someText }}, I get the correct file path (../assets/city.png).我在 display.vue 中显示图像的地方,如果我打印 {{ someText }},我会得到正确的文件路径(../assets/city.png)。

display.vue显示.vue

<template>
    <example :someText="'../assets/' + stamp + '.png'"></example>
</template>

<script>
import example from './example.vue'

export default {
    name: "",
    data() {
        return {
            stamp: "city"
        }
    }
    components: [
        example
    ]
};
</script>

example.vue例子.vue

<template>
    <img :src="someText" />
</template>

<script>
    export default {
      name: "example",
      props: ["someText"]
    };
</script>

You need to change your props case format:您需要更改道具案例格式:

HTML attribute names are case-insensitive, so browsers will interpret any uppercase characters as lowercase. HTML 属性名称不区分大小写,因此浏览器会将任何大写字符解释为小写。 That means when you're using in-DOM templates, camelCased prop names need to use their kebab-cased (hyphen-delimited) equivalents ( source )这意味着当您使用 in-DOM 模板时,camelCased 道具名称需要使用它们的 kebab-cased(连字符分隔)等价物( source

It should work this way:它应该以这种方式工作:

<template>
  <example :some-text="'../assets/' + stamp + '.png'"></example>
</template>

This kind of thing happened to me also.这种事情也发生在我身上。 In your example.vue try this在你的example.vue中试试这个

<template>
    <img src="../assets/city.png" />
</template>

If this not gives you the correct output.如果这没有给你正确的输出。 Then try with the full path然后尝试使用完整路径

<template>
    <img src="src/assets/city.png" />
</template>

My case was image doesn't read from ../assets/city.png but it reads from src/assets/city.png我的情况是图像不是从../assets/city.png读取的,而是从src/assets/city.png

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

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