Webpack 为Vue组件指定默认图像源
我正在构建一个基于VUE JS的图像查看器,其中包含以下脚本Webpack 为Vue组件指定默认图像源,webpack,vuejs2,Webpack,Vuejs2,我正在构建一个基于VUE JS的图像查看器,其中包含以下脚本 <script> import _image from './assets/default.jpg'; export default { name: 'Clickable-Image', props:[ 'isSelected', 'imageSource', 'imageName', ],
<script>
import _image from './assets/default.jpg';
export default {
name: 'Clickable-Image',
props:[
'isSelected',
'imageSource',
'imageName',
],
// Component Data.
data(){
return {
defaultImage:_image,
}
},
// Component Computed Properties
computed:{
// Appearance Computation.
containerStyle : function(){
return{
ui:true,
raised:true,
very:false,
padded:true,
blue: !this.isSelected,
green: this.isSelected,
inverted:true,
segment:true,
}
},
// Image to show.
displayImage(){
// This is working if I send the raw image URL
//return "http://i.imgur.com/N9yfg3C.jpg";
var src = "";
if(this.imageSource == "")
return this.defaultImage;
else
return this.imageSource;
}
},
// Component Methods for Event Handling.
methods:{
ToggleSelection : function() {
if(this.isSelected)
this.isSelected=false;
else
this.isSelected=true;
}
}
}
</script>
从“./assets/default.jpg”导入_图像;
导出默认值{
名称:“可点击图像”,
道具:[
"当选",,
“图像源”,
“imageName”,
],
//组件数据。
数据(){
返回{
默认图像:_图像,
}
},
//组件计算属性
计算:{
//外观计算。
containerStyle:function(){
返回{
ui:是的,
是的,
非常:错,
帕德德:是的,
蓝色:!这是我选的,
格林:这是我当选的,
倒:是的,
片段:对,
}
},
//要显示的图像。
displayImage(){
//如果我发送原始图像URL,这将起作用
//返回“http://i.imgur.com/N9yfg3C.jpg";
var src=“”;
如果(this.imageSource==“”)
返回此.defaultImage;
其他的
返回此.imageSource;
}
},
//用于事件处理的组件方法。
方法:{
切换选择:函数(){
如果(本次选举)
这个.isSelected=false;
其他的
这个.isSelected=true;
}
}
}
我想做的是,当没有图像URL作为输入提供时,在组件中显示一个默认图像
但是,在computed函数中,我无法返回默认图像。当我返回带有图像URL的字符串时(如代码注释中所示),将显示图像
注意:当我在computed displayImage()函数中“return\u image”时,将显示默认图像
这是我的组件模板
<template>
<div v-on:click="ToggleSelection()">
<div v-bind:class="containerStyle">
<div class="ui centered card">
<div class="image">
<img v-bind:src="displayImage">
</div>
</div>
</div>
</div>
</template>
以下是我的项目结构:
我将Vue Cli与webpack simple template starter一起使用。我在这里遗漏了什么?而不是
this.defaultImage代码>像这样返回图像文件的路径return'/assets/default.jpg'
而不是this.defaultImage代码>像这样返回图像文件的路径<代码>返回'/assets/default.jpg'