简体   繁体   English

为什么 Vue.js 图像组件不起作用?

[英]Why Vue.js image component is doesn't work?

I have something problem.我有问题。 I wanna make a Vue.js Component.我想做一个 Vue.js 组件。 this component show something image.这个组件显示一些图像。 it similar <img> tag.If you know <img> tag, You can understand very easy to this question.它类似于<img> > 标签。如果你知道<img>标签,你可以很容易理解这个问题。

So I want make Vue component like this.所以我想做这样的 Vue 组件。 my code is following below我的代码如下

  props: ['link,des'],
template: '<img :src={{ link }} :alt={{ des }}></img>'
}),

this code is javascipt这段代码是 javascipt

html code is following below html代码如下

  <image link="https://kr.vuejs.org/images/logo.png" des="sans"></image>

pls answer me kindly.请友好地回答我。 Thank you谢谢

Your property props is wrongly set.您的属性props设置错误。 You need to split your string.您需要拆分字符串。

props: ['link', 'des'],

There is several way to do this.有几种方法可以做到这一点。 You can even specify the type of your props :您甚至可以指定道具的类型:

props: {
    link: String, 
    des: String
},

All details can be found in the documentation here : https://v2.vuejs.org/v2/guide/components-props.html#Prop-Types所有详细信息都可以在此处的文档中找到: https ://v2.vuejs.org/v2/guide/components-props.html#Prop-Types

First of all, you can't use the reserved keyword ( image ) as a component.首先,不能使用保留关键字( image )作为组件。 Then you don't need to use curly brackets for the HTML attributes.那么您不需要为 HTML 属性使用大括号。 Here is the working example.这是工作示例。

 Vue.component('my-image', { props: ['link', 'des'], template: `<img :src='link' :alt='des'></img>` }); new Vue({ el: '#app' });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <my-image id='app' link="https://kr.vuejs.org/images/logo.png" des="sans"></my-image>

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

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