我有一个基本的vue.js组件,如下所示:
template:'<nav id="custom-erp-menu-nav">'+
'<ul id="custom-erp-menu-lists">'+
'<li class="custom-erp-menu-list" v-on:click="toggleOpenChild" v-for="module in modules">'+
'<a href="#">'+
'<span>'+
//'<img v-bind:src="assets/images/module-icons/module.icon.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
'<img src="assets/images/module-icons/{{ module.icon }}.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
'</span>'+
'<span class="custom-erp-menu-parent">{{ module.name }}</span>'+
'</a>'+
'<ul class="nav custom-erp-menu-child-dropdown" id="purchase-order-child">'+
'<li><a href="page-profile.html" class="custom-erp-menu-child">Profile</a></li>'+
'<li><a href="page-login.html" class="custom-erp-menu-child">Login</a></li>'+
'<li><a href="page-lockscreen.html" class="custom-erp-menu-child">Lockscreen</a></li>'+
'</ul>'+
'</li>'+
'</ul>'+
'</nav>',
在图像标签中,我尝试像这样放置图像源
'<img src="assets/images/module-icons/{{ module.icon }}.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
这不起作用。 我也尝试过其他类似的答案,
'<img :src="'assets/images/module-icons/'+module.icon.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
这是我的数据
data : function(){
return {
modules : [
{ name : 'Foo', icon : 'dollar-bills'},
{ name : 'Bar', icon : 'dollar-trucks'},
{ name : 'FOOBAR', icon : 'env-env'}
]
}
},
答案 0 :(得分:0)
由于您正在以字符串形式编写组件,并且已经用完了双引号和单引号,因此必须使用template strings/literals.
因此,要解决图像源的一个问题,您必须执行以下操作:
'<img :src="`assets/images/module-icons/${module.icon}.svg`" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'
每个要执行的变量或脚本都必须在美元符号的胡须内。
Vue.component('my-component', {
data: function() {
return {
modules : [
{ name : 'Foo', icon : 'dollar-bills'},
{ name : 'Bar', icon : 'dollar-trucks'},
{ name : 'FOOBAR', icon : 'env-env'}
],
count: 0
}
},
template:'<nav id="custom-erp-menu-nav">'+
'<ul id="custom-erp-menu-lists">'+
'<li class="custom-erp-menu-list" v-on:click="" v-for="module in modules">'+
'<a href="#">'+
'<span>'+
'<img :src="`assets/images/module-icons/${module.icon}.svg`" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
'</span>'+
'<span class="custom-erp-menu-parent">{{ module.name }}</span>'+
'</a>'+
'<ul class="nav custom-erp-menu-child-dropdown" id="purchase-order-child">'+
'<li><a href="page-profile.html" class="custom-erp-menu-child">Profile</a></li>'+
'<li><a href="page-login.html" class="custom-erp-menu-child">Login</a></li>'+
'<li><a href="page-lockscreen.html" class="custom-erp-menu-child">Lockscreen</a></li>'+
'</ul>'+
'</li>'+
'</ul>'+
'</nav>'
})
new Vue({
el: '#app'
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script>
<div id="app">
<my-component/>
</div>
答案 1 :(得分:0)
这就是Vue拥有Computed properties
的原因Vue.component('my-component', {
data: function() {
return {
modules : [
{ name : 'Foo', icon : 'dollar-bills'},
{ name : 'Bar', icon : 'dollar-trucks'},
{ name : 'FOOBAR', icon : 'env-env'}
],
count: 0
}
},
computed: {
assetsPath: function(file) {
return 'assets/images/module-icons/' + file +'.svg';
}
},
template:'<nav id="custom-erp-menu-nav">'+
'<ul id="custom-erp-menu-lists">'+
'<li class="custom-erp-menu-list" v-on:click="" v-for="module in modules">'+
'<a href="#">'+
'<span>'+
'<img :src=assetsPath(module.icon) class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
'</span>'+
'<span class="custom-erp-menu-parent">{{ module.name }}</span>'+
'</a>'+
'<ul class="nav custom-erp-menu-child-dropdown" id="purchase-order-child">'+
'<li><a href="page-profile.html" class="custom-erp-menu-child">Profile</a></li>'+
'<li><a href="page-login.html" class="custom-erp-menu-child">Login</a></li>'+
'<li><a href="page-lockscreen.html" class="custom-erp-menu-child">Lockscreen</a></li>'+
'</ul>'+
'</li>'+
'</ul>'+
'</nav>'
})