VUEJS组件中的图像未加载

发布于 2025-02-05 03:02:09 字数 1460 浏览 3 评论 0原文

我得到了一个父组件,将数据对象发送到这样的孩子组件:

< child object-data =“ url:url:url onere',title:'title''><</child>

然后在我的孩子组件上,我通过执行:

<script>
    export default {
        props: [
            'objectData'
        ]
    }
</script>

现在,出于某种原因,我可以使用标题而没有任何问题{objectdata.title}}}并显示出来。

但是,当涉及IMG标签内的URL时,它不会呈现图像。

我尝试执行以下操作:

&lt; img:src =“ objectdata.url”/&gt;&lt; ----不渲染

&lt; img v-bind:src =“ objectdata.url “/&gt;&lt; ---不渲染

&lt; img v-bind:src =“ require(objectdata.url)”/&gt;&lt; - 引发警告错误因为这不是一条路径,而是一个对象。

&lt; img v-bind:src =“ {objectdata.url}”/&gt;&lt; -----抛出错误

&lt; img v-bind:src ='{{objectdata 。

如果我在没有对象的情况下写下URL,则可以工作。

&lt; img v-bind:src =“ src/assets/images/icon.png”/&gt;

但我希望我的URL来自父组件。

关于如何解决这个问题的想法?我在WebPack文件上添加了URL-LOADER:

            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader'
            }

我还尝试从计算/方法fuction返回objectdata.url fuction:

computed: {
     getImageUrl: function() {
           return objectData.url;
     }
}

然后将其像:src =“ getimageUrl”:src =“ {{getimageurl}}”,我也不幸运。

I got a parent component which sends a data object to the children component like this:

<child object-data=" url: 'url here', title: 'Title'"></child>

Then on my children component, I get this object data by doing:

<script>
    export default {
        props: [
            'objectData'
        ]
    }
</script>

Now, for some reason, I can use title without any problem like this {{ objectData.title }} and shows up.

But when it comes to the URL inside an img tag it's not rendering the image.

I attempted doing the following:

<img :src="objectData.url"/> <--- not rendering

<img v-bind:src="objectData.url"/> <--- not rendering

<img v-bind:src="require(objectData.url)"/> <-- throws a warning error because it's not a path but an object I guess.

<img v-bind:src="{objectData.url}"/> <--- throws an error

<img v-bind:src="{{objectData.url}}"/> <--- throws an error

When I check on the dom element, it doesn't even contain a src attribute afterwards.

If I write down the URL without an object, it works.

<img v-bind:src="src/assets/images/icon.png"/>

But I want my URL to come from a parent component.

Any ideas on how to solve this? I added the url-loader on my webpack file:

            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader'
            }

I also attempted returning objectData.url from a computed/methods fuction:

computed: {
     getImageUrl: function() {
           return objectData.url;
     }
}

And then use it like :src=“getImageUrl” or :src=“{{getImageUrl}}” and I wasn’t lucky either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我遇到了同一问题,并通过使用require函数修复了它:

在parent component app.vue中:

<carousel-posts :posts="posts" />

export default {
 name: "app",
data() {
  return {
   posts: [
     {
      img: require("./assets/logo.png"),
      title: "Title 1",
      subTitle: "Sub Title 1",
      body:"lorem ipsum ..."
    }
    ...
    ]
    };
   }
  ...
 }

在儿童组件中,我通过posts post 并绑定图像src如下:

    <div class="card-body" v-for="(post,idx) in posts" >
      <img class="card-img" :src="post.img" />
         ...
     </div>

        <script>
           export default {
            props: ["posts"],
           ...

因此,在您的情况下,您应该有类似的内容:

     <child :object-data="objectData"></child>

        ...
     data(){ 
          return{
            dataObject:{
              url: require('./assets/someimg.png'), 
              title: 'Title'
             }
            }
          }

更新:

我的项目树:

   src
   |_assets
   |   |_logo.png
   |_components
   |   |_CarouselPosts.vue
   |_App.vue

I faced the same issue and i fixed it by using require function :

in the parent component App.vue :

<carousel-posts :posts="posts" />

export default {
 name: "app",
data() {
  return {
   posts: [
     {
      img: require("./assets/logo.png"),
      title: "Title 1",
      subTitle: "Sub Title 1",
      body:"lorem ipsum ..."
    }
    ...
    ]
    };
   }
  ...
 }

in the child component i loop through the posts and bind the image src as follows :

    <div class="card-body" v-for="(post,idx) in posts" >
      <img class="card-img" :src="post.img" />
         ...
     </div>

        <script>
           export default {
            props: ["posts"],
           ...

So in your case you should have something like :

     <child :object-data="objectData"></child>

        ...
     data(){ 
          return{
            dataObject:{
              url: require('./assets/someimg.png'), 
              title: 'Title'
             }
            }
          }

Update :

my project tree :

   src
   |_assets
   |   |_logo.png
   |_components
   |   |_CarouselPosts.vue
   |_App.vue
睡美人的小仙女 2025-02-12 03:02:09

两种替代方法:

  1. vue解决的静态链接。但是它们基于SRC,并且不使用WebPack / File-Loader:
<img :src="'../assets/filename.png'"/>
  1. 与WebPack一起使用。请注意结尾的“ .default”:
<img :src="require('../assets/filename.png').default"/>

2019年11月的文档相关的文件:
https://github.com/vuejs/vuejs/vue-loader/loader/sissues/1612

Two alternatives:

  1. Static links that vue resolves. But they're src-based and don't work with webpack / file-loader:
<img :src="'../assets/filename.png'"/>
  1. Works with webpack. Note the ".default" at the end:
<img :src="require('../assets/filename.png').default"/>

Documentation relevant effective Nov 2019:
https://github.com/vuejs/vue-loader/issues/1612

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文